使用系统.Data代替System.Data.SqlClient

本文关键字:Data System SqlClient 代替 系统 | 更新日期: 2023-09-27 18:07:50

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

使用系统.Data代替System.Data.SqlClient

如果我没理解错的话,那么没有。您必须明确使用名称空间。如果您想在示例中访问DataTable,则需要包含System。数据-但这不会让您访问任何嵌套的名称空间。

从这里

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