C# 使用System.Data而不是System.Data.SqlClient

C# 使用System.Data而不是System.Data.SqlClient,c#,namespaces,sqlclient,C#,Namespaces,Sqlclient,我有一个问题是如何使用System.Data和System.Data.SqlClient 在下面的示例中,我使用了System.Data.SqlClientnamespace。我是否可以在此处写入System.Data而不是System.Data.SqlClient,因为SqlClient已包含在System.Data命名空间中 我的代码: using System; using System.Data.SqlClient; class Program { static void Ma

我有一个问题是如何使用
System.Data
System.Data.SqlClient

在下面的示例中,我使用了
System.Data.SqlClient
namespace。我是否可以在此处写入
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));
          }
        }
    }
  }
}

如果我理解正确的话,那就不需要了。你必须明确使用名称空间。如果您想在示例中访问DataTable,您需要包括System.Data,但这并不能让您访问任何嵌套的名称空间

创建using指令以使用命名空间中的类型,而不使用 必须指定名称空间。using指令不会为您提供 访问嵌套在指定命名空间中的任何命名空间