簡體   English   中英

DbDataAdapter.Fill 是否保證返回的表名是 Table、Table1、Table2 等?

[英]Does DbDataAdapter.Fill guarantee returned table names to be Table, Table1, Table2 etc.?

來自DBDataAdapter.Fill 的 MS 文檔

當指定的查詢返回多個結果時,返回查詢的每一行的結果集被放在一個單獨的表中。 其他結果集通過將整數值附加到指定的表名稱來命名(例如,“Table”、“Table1”、“Table2”等)。

我可以從DataSet的示例中看到他們使用Table

//Create a SqlDataAdapter for the Suppliers table.
SqlDataAdapter adapter = new SqlDataAdapter();

// A table mapping names the DataTable.
adapter.TableMappings.Add("Table", "Suppliers");

// Open the connection.
connection.Open();
Console.WriteLine("The SqlConnection is open.");

// Create a SqlCommand to retrieve Suppliers data.
SqlCommand command = new SqlCommand(
    "SELECT SupplierID, CompanyName FROM dbo.Suppliers;",
    connection);
command.CommandType = CommandType.Text;

// Set the SqlDataAdapter's SelectCommand.
adapter.SelectCommand = command;

// Fill the DataSet.
DataSet dataSet = new DataSet("Suppliers");
adapter. Fill(dataset);

但是Table、Table1、Table2等等有保障嗎? 當我想使用 TableMappings 為 DataSet 表設置自定義名稱時,我可以安全地使用它們嗎?

給定以下兩個表(在 SQL Server 中):

雇員

CREATE TABLE Employee(Id int not null Identity(1,1),
                      FirstName varchar(50),
                      LastName varchar(50),
                      CONSTRAINT PK_Employee_Id Primary Key(Id));

資產

CREATE TABLE Asset(Id int Identity(1,1),
                   Category varchar(25),
                   Name varchar(50) not null,
                   Description varchar(125),
                   EmployeeId int,
                   CONSTRAINT PK_Asset_Id Primary Key(Id),
                   Constraint FK_Asset_EmployeeId_Employee_Id Foreign Key(EmployeeId) references Employee(Id));

嘗試以下操作:

添加以下使用指令

  • using System.Configuration;
  • using System.Data;
  • using System.Data.SqlClient;

應用程序配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="ConnectionStringSqlServerAuth" connectionString="Server=.\SQLExpress;Database=HR;User Id=testUser;Password=mySuperSecretPassword;" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
</configuration>

獲取數據

private string _connectionStr = ConfigurationManager.ConnectionStrings["ConnectionStringSqlServerAuth"].ConnectionString;

public DataSet GetData()
{
    //create new instance
    DataSet ds = new DataSet();

    using (SqlConnection con = new SqlConnection(_connectionStr))
    {
        using (SqlDataAdapter da = new SqlDataAdapter())
        {
            da.SelectCommand = new SqlCommand("SELECT * FROM Employee", con);
            da.Fill(ds, "Employee");

            da.SelectCommand = new SqlCommand("SELECT * FROM Asset", con);
            da.Fill(ds, "Asset");

            foreach (DataTable dt in ds.Tables)
            {
                Debug.WriteLine($"dt[{dt.TableName}] row count: {dt.Rows.Count}");
            }
        }

        return ds;
    }
}

資源

其他資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM