1
我有一个如何使用System.Data
和System.Data.SqlClient
的问题。使用System.Data而不是System.Data.SqlClient
在以下示例中,我使用了System.Data.SqlClient
namesspace。我可以在这里写System.Data
而不是System.Data.SqlClient
,因为SqlClient是已经包含在System.Data
命名空间吗?
我的代码:
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
// First access the connection string.
// ... This may be autogenerated in Visual Studio.
string connectionString =
ConsoleApplication1.Properties.Settings.Default.ConnectionString;
//
// In a using statement, acquire the SqlConnection as a resource.
//
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));
}
}
}
}
}