Connectivity With Database In Windows Forms Application Using F#

Introduction

This article explains how to work with Windows Forms applications in F# and how to import the namespaces. In addition you will see how to connect with a SQL Server database from the Windows Forms application and how to add user controls in the Windows Forms application. I have shown how to import the essential namespaces in F#.

At first to create the database in SQL Server, write the following code in database SQL Server.

create database  UserInformation

use UserInformation

create table EmployeeInfo(

EmpId int,

FirstName varchar(30),

LastName varchar(30),

Salary money,

Address nvarchar(max),

City varchar(30)

)

Now use the following steps in Visual Studio 2012.

Step 1:

Open Visual Studio 2012 and select "New project" --> "Visual F#" -> "F# console application" to create your F# application.

select-application.jpg

Step 2:

In this step you need to go at solution explorer and right-click on references and select the add references see the figure given below

add-ref.jpg

Step 3:

After selecting "Add References", in the framwork template you need to select System.Windows.Forms, System.Drawing and System.Data while holding down the Ctrl key and Click on "Ok." 

import-namespaces.jpg

Step 4:

Write the following code in the F# console application.

//Importing Namespace

open System

open System.Windows.Forms

open System.Drawing

open System.Data.SqlClient

open System.Data

// Creating Form & User Controls

// Form

let MyForm = new Form(Height =400, Width = 500)

//   Labels

let lbl1 = new Label(Top =20 , Height =30, Width = 50)

let lbl2 = new Label(Top =50 , Height =30, Width = 100)

let lbl3 = new Label(Top =80 , Height =30, Width = 100)

let lbl4 = new Label(Top =110 , Height =30, Width = 50)

let lbl5 = new Label(Top =140 , Height =30, Width = 50)

let lbl6 = new Label(Top =170, Height =30, Width = 50)

//   TextBoxes

let txt1 = new TextBox(Top =20, Left =120)

let txt2 = new TextBox(Top =50, Left =120)

let txt3 = new TextBox(Top =80, Left =120)

let txt4 = new TextBox(Top =110,Left=120)

let txt5 = new TextBox(Top =140,Left=120)

let txt6 = new TextBox(Top =170,Left=120)

lbl1.Text <- "ID:"

lbl2.Text <- "First Name :"

lbl3.Text <- "Last Name :"

lbl4.Text <- "Salary"

lbl5.Text <- "Address"

lbl6.Text <- "City"

let btn1 = new Button(Top =200, Left =80, Height =40, Width =60)

btn1.Text <- "SUBMIT"

//Adding Controls to Form

MyForm.Controls.Add(lbl1)

MyForm.Controls.Add(lbl2)

MyForm.Controls.Add(lbl3)

MyForm.Controls.Add(lbl4)

MyForm.Controls.Add(lbl5)

MyForm.Controls.Add(lbl6)

MyForm.Controls.Add(txt1)

MyForm.Controls.Add(txt2)

MyForm.Controls.Add(txt3)

MyForm.Controls.Add(txt4)

MyForm.Controls.Add(txt5)

MyForm.Controls.Add(txt6)

MyForm.Controls.Add(btn1)

//Database Connectivity

let constr = @"Data Source=.;Initial Catalog=UserInformation; uid=sa; pwd=password@123; Integrated Security=True"

let con = new SqlConnection(constr)

let com1 = new SqlCommand()

let com2 = new SqlCommand()

let com3 = new SqlCommand()

let com4 = new SqlCommand()

let ds = new DataSet()

 //Clear the TextBoxes

btn1.Click.Add(fun _->

txt1.Clear()

txt2.Clear()

txt3.Clear()

txt4.Clear()

txt5.Clear()

txt6.Clear()

txt1.Focus()|>ignore

)

 //Insert record into DataBase sqlserver

btn1.Click.Add(fun _->

con.Open()

com1.Connection <- con

com1.CommandText <- "insert into EmployeeInfo values"+txt1.Text+",'"+txt2.Text+"','"+txt3.Text+"','"+txt4.Text+"','"+txt5.Text+"','"+txt6.Text+"'"

MessageBox.Show("Record Inserted Successfully")|>ignore

com1.ExecuteNonQuery()|> ignore

con.Close()

)

Application.Run(MyForm)

Step 5:

Debug the application by pressing F5 to execute the Windows Forms application.

Output

output.jpg

Output in database

output-database.jpg

Summary

This article explained how to connect with the SQL Server database and work with the Windows Forms application using F#. The article also showd how the values are saved from the Windows Forms application to a SQL Server database.


Similar Articles