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

时间:2015-08-14 07:07:55

标签: c# namespaces sqlclient

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

在以下示例中,我使用了System.Data.SqlClient namesspace。我可以在这里写System.Data而不是System.Data.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));
          }
        }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

如果我理解正确,那么不。你必须明确命名空间。如果您想在示例中访问DataTable,则需要包含System.Data - 但这不会允许您访问任何嵌套的命名空间。

来自here

  

创建using指令以在没有命名空间的情况下使用类型   必须指定命名空间。 using指令不会给你   访问嵌套在您指定的命名空间中的任何命名空间。