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
2009-12-11 03:11:46
Wow, I didn't realize you could use two variables in a using statement. Does that translate to C# also?
Mike C.
2009-12-11 03:16:56
Sort of: C# does allow this but the exact syntax is a little different.
Joel Coehoorn
2009-12-11 03:22:38
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
2009-12-11 03:27:39
For extra fun, try a `using () try \n { \n } \n catch() {}`
Joel Coehoorn
2009-12-11 03:28:33