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.