Thursday 6 February 2014

Read a Data from a Database using F#

In this article we are going to see how to read a data from the database in F#. We are going to read the data using .Net framework class .

SQL :-
  
create table test
(
id int identity(1,1),
name varchar(100),
occupation varchar(100)
)

insert into test(name,occupation)
values ('KA','AT')




F# :-

open System.Data.SqlClient
let readddata =
use conn = new SqlConnection(@"Data Source = rajesh-PC\sqlserver2008; Initial Catalog = rajesh; Integrated Security=true")
use cmd = new SqlCommand("select id,name,occupation from test", conn)
conn.Open()
use rdr = cmd.ExecuteReader()
while rdr.Read() do
       printfn " Id:- %s  Name:- %s Occupation:- %s " (rdr.GetInt32(0).ToString())                  (rdr.GetString(1)) (rdr.GetString(2)) 

output: 

 Id:- 1  Name:- Suresh  Occupation:- EP
 Id:- 2  Name:- Sam     Occupation:- DE
 Id:- 3  Name:- KA      Occupation:- AT


From this post you can learn how to read the data from the Database.

No comments:

Post a Comment