2015-08-14 37 views
1

我有一个如何使用System.DataSystem.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)); 
      } 
     } 
    } 
    } 
} 

回答

2

如果我理解正确的话则没有。你必须明确命名空间。如果你想在你的例子中访问DataTable,你需要包含System.Data - 但是这不会让你访问任何嵌套的命名空间。

here

,而不必指定命名空间 创建一个使用指令命名空间中的使用类型。使用指令不会为您提供对嵌套在您指定的命名空间中的任何命名空间的访问权限 。