Getting Started with F# (2)

So I finally got my project created and saved in F# (see my prior post)

I now need to do something.

I figured the first thing I would bite off would be my database access.  This always seems to trip me up when I learn a new language.

I know I will need ADO.Net, so the first thing I do is add a reference to my project for System.Data.Sqlclient.  (I am using MS SQL Server as my data store.)

Here is my first F# Code!

/----------------------------------------------------------------------------

#light
open System
open System.Collections.Generic
open System.IO
open System.Data.SqlClient

let servername = "BIG_ROCK\LOGGERSEDGE"
let dbname = "SMDATA"
let connstr = "Data Source=" + servername + "; Initial Catalog=" + dbname + ";" + _
    User ID=sa; PASSWORD=XXXXXX;"

let conn = new System.Data.SqlClient.SqlConnection(connstr)
let iOpen = conn.Open()

let sql = "SELECT DISTINCT INDUSTRY_ORDER, INDUSTRY_NAME FROM VL_INDUSTRIES WHERE GROUP_TYPE_ID = 1 AND INDUSTRY_ORDER > 0 "
let cmd = new System.Data.SqlClient.SqlCommand(sql, conn)

let reader = cmd.ExecuteReader()
while reader.Read() do
    printfn "Industry Name = %s " (reader.GetString(1))

/-----------------------------------------------------------------------------------------

The code is really pretty basic.  I create my connection string and connect to my database table that has all my industries.  I then print them to the console.  Note that the "+" operator works as the concatenation operator for strings in F#.

There were only a few things that tripped me up

(1)  F# is case sensitive.  I come from the VB world where case doesn't matter.  VB will even fix your text for you.  Not F#.  You need to make sure all capital letters are capitalized and lower case are lower cased.

(2)  The assignment operator.  Every "variable" is assigned a value using the "let" operator. Eg. let x = 5.  I put the double quotes around variable because in F# the value of a variable can't change, so it acts more like a constant than a true variable.  In F# lingo they call this immutable.

(3)  Indenting.  Indenting is key in F#.  Basically, Indenting takes the place of the curly braces {} in C#.  For example, in my code:

while reader.Read() do
    printfn "Industry Name = %s " (reader.GetString(1))

the printfn statement is indented (4 spaces) in my case.  You cannot use tabs -- you must use spaces.  If I wanted to add more logic to my loop, I would align the additional statements to the printfn statement.  When I finish my logic, I just return to the left margin and continue on.

(4)  What is the devil with the parentheses around the reader.GetString() function?  Who knows?  But it seems that an argument in a function call that is a function itself needs to be wrapped in parentheses.

(5)  Running the App.  You can run an F# application by selecting the text you want to execute and click ALT-ENTER.  This will send the output to the F# interactive window.  This is sweet for testing.  I ran the above code successfully in the the F# interactive window.   You can also run the App by clicking F5.  This runs the whole ball of wax.  This is where I crashed and burned.  When I ran the project either using F5 or clicking on the .exe, I kept getting an error telling me that 'no entry point was found' for dbnetlib.dll.  Obviously, there was something wrong with my data access code.  But what?  I poked around on the Web for 30 minutes and was frustrated at every turn. 

Finally, it occurred to me that I should install Visual Studio 2008 Service Pack 1.  (I was using the RTM version).  Now this is a project when you have nothing better to do 'cause it takes a couple of hours to download and then install.  But Voila! it fixed my problem.

So, the moral is, if you use F# you need to get Visual Studio 2008 SP 1.

 

 

 

Print | posted @ Thursday, June 18, 2009 4:34 PM

Comments on this entry:

Gravatar # plus size wedding dresses
by plus size wedding dresses at 1/30/2012 12:07 AM

It is absolutely basal access a disney wedding gowns for the absolute acknowledgment policy. this could be accurately 18-carat for individuals which accept any blazon of apropos apropos the gown.2- Do alone a little cogent analysis to access in a adjustment to admit what could be advancing as able-bodied as actualize a plus size wedding dressesat identical time to access the adeptness to access one of one of the a lot of benign deals.3- bearing appliance of the array of on the Evening Dresses, on top of that it is acute that you artlessly be specific that the abundance your breadth bearing the acquirement could be genuine. be specific they've acquired disney wedding dress as able-bodied as an advantageous number. If in any way possible, seek account from abreast abutting accompany and as pertaining to references antecedent to bearing one even added alternative. an ideal action added importantly, be acquainted that the huge bulk of accoutrements are beautiful accordingly with cheap plus size wedding dressesdesigned to amuse the blueprint of altered people.
  
Gravatar # Medical biller
by Medical biller classes Los angel at 1/31/2012 12:55 AM

Thank you a lot for experiencing this appeal publish with me. I am appreciating it very much! Excited to one more wonderful publish. Satisfaction in to the author! All the best..
Medical biller classes Los angeles
  
Gravatar # re: Getting Started with F# (2)
by air max pas cher at 2/2/2012 11:17 PM

Cortez brochure ont sans aucun doute devenir la première marque de chaussures de course. Depuis 1971," coureur du monde ?, publié? chaussures coureur "brochure a été, nike air max classic bw vetements pour homme nike air max Nike chaussures de course à cause de l'émergence soudaine du marché a subi d'énormes changements. Bauman avec la société Onitsuka directement, Cortez Johnson estime que le projet a re?u le numéro de Bowman une priorité, sentirez plus comme une paire de chaussures de course, nike air max classic bw boutique pour homme comme un matériau de rembourrage lié directement à vous de la même sous leurs pieds. Amortissement est l'une des utilisations les plus élémentaires d'une paire de chaussures. Chaque fois que vous sautez à l'automne sur le sol, achat la redoute classic bw le genou pour résister à sept fois la pression habituelle. 1, le stock Nike revient depuis 1986, le taux de croissance annuelle moyenne de 47% dans la période 1986-1996, la décharge "Fortune" magazine de 1000 entreprises américaines, achat foot locker classic bw la société au sein du top 10. Le secret? Si l'étude en profondeur de la philosophie de l'entreprise se trouvent, de prendre des risques, briser la routine, mettant la compagnie une raison importante pour un miracle. Par exemple, les chaussures de badminton sont vendus en Asie, achat shox classic bw vendu à une nouvelle ?le digne de la chaussure de handball scandinave. Cependant, ce processus est plein de difficultés. Knight est de retour" à mi-Février, dont 8 bit est le directeur de succursale à l'étranger. Lors de la réunion, il a annoncé en Juin, annonce air max classic bw succèdera à 42 ans, Clark, président de Nike 66 ans Charles De off Duona. Chevalier la plupart du temps dans le bureau à c?té d'une petite pièce. Les vêtements sales Hut gisant sur le sol, magasin air max classic bw à c?té de tas de choses et de documents, pratiquement personne d'autre dans sa maison, Chevalier plaint que: ?Une fois que les gens dans votre maison, ils seront toute la journée et à, boutique air max classic bw Chevalier a voulu créer un environnement de travail harmonieux.
  
Gravatar # medical office administrator classes San Bernardino
by medical office administrator at 2/5/2012 11:27 PM

medical office administrator classes San BernardinoI’ve analysis a few awesome problems here. Definitely team look for developing for returning to. I ask yourself how much function you set to create such a awesome useful web page.

  
Gravatar # wholesale dealers license
by wholesale dealers license at 2/6/2012 12:47 AM

wholesale dealers license
very nice and informative blog that you post here, I found a very good after reading it seems that you have put your extra effort to completely stop what I would like to thank you for sharing with us dear, for good and for us to aspire to protect.....
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 6 and 2 and type the answer here: