open System.Data open System.Data.SqlClient let cmd (s:string) :SqlCommand = new SqlCommand() cmd.Connection <- new SqlConnection(s) cmd.Connection.Open()

The problem is the first line

1
let cmd (s:string) : SqlCommand = new SqlCommand()

defines a function that takes a string and returns a SqlCommand (string -> SqlCommand). The next line

1
cmd.Connection <- new SqlConnection(s)

Attempts to assign a value to a *function*, not to a member of a SqlCommand. To get this to compile, you could write (incorrectly)

1
cmd("a string").Connection <- new SqlConnection()

I think what you want to write is something more like the following:

1
2
3
4
5
let dordr (conn:string) (query:string) =
    use cmd = new SqlCommand(query)
    cmd.Connection <- new SqlConnection(conn)
    cmd.Connection.Open()
    cmd.ExecuteReader()

Or, better (not tested and I gotta get to work)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let read (conn:string) (query:string) =
    seq {
        use cmd = new SqlCommand(query)
        cmd.Connection <- new SqlConnection(conn)
        cmd.Connection.Open()
        use rdr = cmd.ExecuteReader()
        while rdr.Read() do
            yield rdr
        }

read "connection string" "query"
|> Seq.iter( fun rdr ->
    (* do something with the reader *)
    ()
    )
By on 6/28/2010 8:39 AM ()

Thanks for the explanation. Now I follow the error (cmd is a function not an object)

Is there a way to set cmd to an instantiated object?

By on 6/28/2010 11:45 AM ()

Thanks for the explanation. Now I follow the error (cmd is a function not an object) Is there a way to set cmd to an instantiated object?

Sure. Don't specify a parameter in your definition of "cmd", just bind an object instance to it, as in:

1
2
3
4
5
6
7
8
9
10
open System.Data
open System.Data.SqlClient

let query = "SELECT * from foo"
let conn  = "blah"

use cmd = new SqlCommand(query)

cmd.Connection <- new SqlConnection(conn)
cmd.Connection.Open()

Note that the above specifies "use" rather than "let". This ensures that the object is Disposed when it goes out of scope, in the same way that "using (blah) { foo() }" works in C#.

By on 6/29/2010 7:59 AM ()
1
2
3
4
5
6
open System.Data
open System.Data.SqlClient

let cmd (s:string) :SqlCommand = new SqlCommand()
cmd.Connection <- new SqlConnection(s)    (* Displayed incorrectly above*)
cmd.Connection.Open()

You know Foxjazz I don't mean to be harsh but maybe you could tell us what is occurring to lead you to believe this isn't working? Perhaps share the error message you're getting or share whatever it is that makes you think this isn't doing what you mean it to do?

And perhaps you could meet all of us halfway and try using the <code> tags to format your code. The line I commented above isn't displayed correctly in your original message.

By on 6/23/2010 1:44 PM ()

OnorioCatenacci I don't mean to be harsh (starting a reply based on this implies harshness doesn't it?)

I figure anyone with f# had the ability to copy the five lines of code and paste it int a .fs file and see the "error"for themselves.

Since I guess that isn't the case, the message is:

"Error 1 The field, constructor or member 'Connection' is not defined

Be harsh, I don't care, just looking for answers.

By on 6/28/2010 8:15 AM ()

OnorioCatenacci I don't mean to be harsh (starting a reply based on this implies harshness doesn't it?)

No, in this particular case, it implies that I realize that what I'm saying may sound harsh but I'm really trying to help.

I don't know about your experience but in my experience developers can sometimes get defensive when someone points out that the way they're asking their question isn't conveying the question as well as they think it is.

I figure anyone with f# had the ability to copy the five lines of code and paste it int a .fs file and see the "error"for themselves.

This is part of the point I was trying to convey to you. I copy the "five lines of code" you posted (which show up as four lines in your original posting) into my FSI and this is, verbatim, what I see:

> open System.Data

- open System.Data.SqlClient

-

- let cmd (s:string) :SqlCommand = new SqlCommand()

- cmd.Connection cmd.Connection.Open();;

cmd.Connection cmd.Connection.Open();;

---------------^^^^^^^^^^^^^^^^^^^^^

stdin(5,16): error FS0597: Successive arguments should be separated by spaces or

tupled, and arguments involving function or method applications should be parenthesized

As you can see, I get an error but I don't get the error that you get because I don't have the same code that you have in your environment. And it wouldn't matter if I copied the lines into a .fs file and compiled them--I'd still get the FS0597 error.

Formatting is especially important in a language which uses whitespace to determine scope.

Since I guess that isn't the case, the message is:

"Error 1 The field, constructor or member 'Connection' is not defined

Be harsh, I don't care, just looking for answers.

May I suggest that you'll find it hard to get the right answers until people can figure out exactly what it is that you're asking? Formatting the code is one way to make sure your code is presented correctly so that others can see exactly what it is that you're trying to figure out.

Even if you had originally posted your code formatted correctly, it may be that other people with different versions of F# (there are a few different versions floating around) may get a slightly different error message or behavior than you do. It sometimes helps others to suggest a good course of action if they know that they're seeing a different behavior than the original poster is seeing.

By on 6/28/2010 1:43 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper