我在网上找到了以下代码:
using (SqlConnection con = new SqlConnection(connectionString))
{
//
// Open the SqlConnection.
//
con.Open();
//
// The following code uses an SqlCommand based on the SqlConnection.
//
using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("{0} {1} {2}",
reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
}
}
}
Run Code Online (Sandbox Code Playgroud)
谁能解释一下为什么使用 (SqlCommand ..) 不以分号结尾。我的第二个问题通常是在使用之后我们必须有 { } 来指示使用变量的范围为什么在这种情况下它丢失了?以及如何、何时处理命令对象?