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

Using System.Data Instead of System.Data.SqlClient

我有一个关于如何使用 System.DataSystem.Data.SqlClient 的问题。

在下面的示例中,我使用了 System.Data.SqlClient 命名空间。我可以在这里写 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 - 但这不会让您访问任何嵌套的命名空间。

来自 here

Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.