Saving Records By ExecuteNonQuery() in FSharp


Introduction:

ExecuteNonQuery() is a method of the Command Class in .NET. It does not return a record. Generally, it is used to store a record from the front end to the DataBase.
We create a DataBase and create an F# application to use ExecuteNonQuery().

In this example, DataBase name is STUDENT and STUDENT_DATAIL is Table name. It has four columns as Roll_No, F_Name, L_Name and Course. Write the following SQL command to create DataBase and Table and creating StoredProcedure.

Creating DataBase and Table:

create database STUDENT

use
STUDENT

create
table STUDENT_DETAIL
(

Roll_No int,
F_Name varchar(20),
L_Name varchar(20),
Course varchar(10)

)

Creating Stored Procedure:

CREATE PROCEDURE MYSTOREPROCEDURE
(

 @Roll_No int,
 @F_Name varchar(20),
 @L_Name varchar(20),
 @Course varchar(10)

)

AS
BEGIN
INSERT
INTO student_DETAIL VALUES(@Roll_No, @F_Name, @L_Name, @Course)
END

Creating F# Application:

Now we create a F# application. For doing it, follow given steps,

Step 1: Take a F# application.

ExecuteNonQuery() in f#

Step 2: We go-to Solution Explorer and Right Click on References. 

ExecuteNonQuery() in f#

Step 3: Click on Add References. Then a pop-up window with caption Add Reference will open as in the below figure.

ExecuteNonQuery() in f#

Step 4: Click at .Net on Add Reference window and select System.Windows.Forms, System.Drawing and System.Data with holding down Ctrl key and Click on Ok.

ExecuteNonQuery() in f#

Step 5: Now we write F# code using ExecuteNonQuery() to store record into DataBase. Here in this example, I am storing record into STUDENT_RECORD table to which we have created earlier (Look at Above SQL statement). 

// Learn more about F# at http://fsharp.net
//importing namespace

open
System
open
System.Drawing
open
System.Data.SqlClient
open
System.Windows.Forms
open
System.Data
//connection string

let
constring = @"Data Source=SERVER_NAME;Initial Catalog=STUDENT;Integrated Security=True"
//Creating user controls

let
form = new Form()
let
txt1 = new TextBox(Top = 10, Left = 70)
let
txt2 = new TextBox(Top = 30, Left = 70)
let
txt3 = new TextBox(Top = 50, Left = 70)
let
txt4 = new TextBox(Top = 70, Left = 70)
let
btn = new Button(Top = 100)
let
lbl1 = new Label(Top = 10, Left = 0, Height = 20)
let
lbl2 = new Label(Top = 30, Left = 0, Height = 20)
let
lbl3 = new Label(Top = 50, Left = 0, Height = 20)
let
lbl4 = new Label(Top = 70, Left = 0, Height = 20)
//form caption

form.Text <- "Saving Record Into DataBase"
 
btn.Text <- "Save"
lbl1.Text <- "ID"
lbl2.Text<- "First Name"
lbl3.Text <- "Last Name"
lbl4.Text <-
"Course"

//Adding user control

form.Controls.Add(txt1)
form.Controls.Add(txt2)
form.Controls.Add(txt3)
form.Controls.Add(txt4)
form.Controls.Add(lbl1)
form.Controls.Add(lbl2)
form.Controls.Add(lbl3)
form.Controls.Add(lbl4)
form.Controls.Add(btn)

//creating SqlConnection

let
con = new SqlConnection(constring)
//open connection

con.Open()
let com = new SqlCommand()
com.Connection <- con
com.CommandType <- CommandType.StoredProcedure
om.CommandText <- "MYSTOREPROCEDURE"
 
btn.Click.Add( fun _ ->
com.Parameters.AddWithValue("@Roll_No", txt1.Text ) |> ignore// passing values
com.Parameters.AddWithValue("@F_Name", txt2.Text ) |> ignore
com.Parameters.AddWithValue("@L_Name", txt3.Text ) |> ignore
com.Parameters.AddWithValue("@Course", txt4.Text ) |> ignore

com.ExecuteNonQuery() |> ignore //ExecutrNonQuery
MessageBox.Show("Saved") |> ignore //passing message
txt1.Clear() //clear TextBox1
txt2.Clear() //"  "
txt3.Clear() // " "
txt4.Clear() // " "
txt1.Focus() |> ignore)
// setting focus

Application.Run(form)

Step 6: Now we run our application. 

ExecuteNonQuery() in f#

We fill the form and Click on Save (Button). Then, a MessageBox will be shown with message "Saved". 

ExecuteNonQuery() in f#

We Click on Ok Button and do some other entries in TextBoxes.

Now we check the saved record. We open SQL Server and write the following SQL statements

SELECT * FROM STUDENT_DETAIL

We look that all record are saved in DataBase.

 ExecuteNonQuery() in f#

Up Next
    Ebook Download
    View all
    Learn
    View all