views:

47

answers:

3

I mean the imports keyword on the uppermost part of the program. What should I type in there?

+1  A: 
Imports System.Data
Imports System.Data.SqlClient

Then here's an example of how to talk to the db:

Using cn As New SqlConnection("connection string here"), _
      cmd As New SqlCommand("Sql statements here")

    cn.Open()

    Using rdr As SqlDataReader = cmd.ExecuteReader()
        While rdr.Read()
            ''# Do stuff with the data reader
        End While
    End Using
End Using

and a c# translation, because people were curious in the comments:

using (var cn = new SqlConnection("connection string here"))
using (var cmd = new SqlCommand("Sql statements here"))
{
    cn.Open();
    using (var rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
            // Do stuff with the data reader
        }
    }
}
Joel Coehoorn
Wow, I didn't realize you could use two variables in a using statement. Does that translate to C# also?
Mike C.
Sort of: C# does allow this but the exact syntax is a little different.
Joel Coehoorn
The C# works because of the normal rules about { } blocks: if you only have one statement you don't need the braces. You can also comma-separate multiple objects in a single using directive in C#, but when you do that they have to all be of the same type, so this typically works better.
Joel Coehoorn
For extra fun, try a `using () try \n { \n } \n catch() {}`
Joel Coehoorn
+2  A: 

You mean:

Imports System.Data.SqlClient
Paul Sasik
A: 
Imports System.Data.SqlClient
cxfx