具有实际表名的数据集

发布于 2024-11-02 00:28:38 字数 681 浏览 2 评论 0原文

我正在尝试将数据从存储过程获取到数据集中。问题是,在数据集可视化工具中,实际的表名称(即客户或员工)不仅仅显示 Table1 、 table2 等。 是否可以获得实际的表名?

   using (SqlConnection sqlConnection = new SqlConnection("Data Source=myserver;Initial Catalog=Northwind;Integrated Security=True"))
        {
            sqlConnection.Open();

            SqlDataAdapter da = new SqlDataAdapter("EXECUTE  [Northwind].[dbo].[GetCustomers_Employees] ", sqlConnection);
            DataSet ds = new DataSet();
            da.Fill(ds);
        }

CREATE PROCEDURE GetCustomers_Employees
AS
BEGIN
SELECT top 10 * from customers
select top 10 * from Employees
END

数据集可视化工具

i am trying to get data from my stored proc into my dataset. The problem is that in the dataset visualizer the actual table name ie customers or employees does not show up just Table1 , table2 etc.
Is it possible to get the actual table names?

   using (SqlConnection sqlConnection = new SqlConnection("Data Source=myserver;Initial Catalog=Northwind;Integrated Security=True"))
        {
            sqlConnection.Open();

            SqlDataAdapter da = new SqlDataAdapter("EXECUTE  [Northwind].[dbo].[GetCustomers_Employees] ", sqlConnection);
            DataSet ds = new DataSet();
            da.Fill(ds);
        }

CREATE PROCEDURE GetCustomers_Employees
AS
BEGIN
SELECT top 10 * from customers
select top 10 * from Employees
END

dataset visualizer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

允世 2024-11-09 00:28:38

您可以在执行填充操作时添加名称,如下所示:

da.Fill(ds, "MyTable");

从那时起,您可以将表引用为

ds.Tables["MyTable"];

而不是使用整数索引(即ds.Tables[0]

请参阅这里:http://msdn.microsoft.com/en-us/library/bh8kx08z(v=VS.100).aspx

编辑:

在您的情况下,您可以使用 TableName 属性,如下所示:

da.Fill(ds);
ds.Tables[0].TableName = "Customers";
ds.Tables[1].TableName = "Employees";

这是一种快速而肮脏的方法,但不是很通用。不幸的是,无法从 SP 获取表的名称,而这可能正是您想要的。一种方法是修改 SP 以返回输出参数:

CREATE PROCEDURE GetCustomers_Employees
   @tableNames varchar(20) OUTPUT
AS
BEGIN
    SET @tableNames = 'Customers,Employees'
    SELECT top 10 * from Customers
    SELECT top 10 * from Employees
END

但是要利用此功能,您还必须修改 SqlDataAdapter 以处理带有输出参数的存储过程:

using (SqlConnection = ...)
    {
       // sqlConnection.Open(); // Not really needed. Data Adapter will do this.

        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetCustomers_Employees";
        cmd.Connection = sqlConnection;

        // Create the parameter object and add it to the command
        SqlParameter param = new SqlParameter("@tableNames", SqlDbType.VarChar);
        param.Direction = ParameterDirection.Output;
        cmd.Parameters.Add(param);

        // Get the Data
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet(); 
        da.Fill(ds);

        // Set the names of the tables in the dataset
        string strTableNames = cmd.Parameters["@tableNames"].Value.ToString();
        string[] tableNames = strTableNames.split(',');

        for (int i=0; i<tableNames.Length; i++)
        {
            ds.Tables[i].TableName = tableNames[i];
        }
    }

请注意上面的代码将处理返回的任意数量的表,因此您可以轻松地将其封装在一个函数中,您可能会发现该函数很有用:

DataSet function(string storedProcedureName, string connectionString)
{
    DataSet ds = new DataSet();
    ... // code above, without DataSet declaration
    return ds;
}

You can add a name when you do the fill operatation, like this:

da.Fill(ds, "MyTable");

From that point forward, you can refer to the table as

ds.Tables["MyTable"];

instead of using the integer index (i.e.ds.Tables[0])

See here: http://msdn.microsoft.com/en-us/library/bh8kx08z(v=VS.100).aspx

EDIT:

In your case, you could use the TableName property, like this:

da.Fill(ds);
ds.Tables[0].TableName = "Customers";
ds.Tables[1].TableName = "Employees";

That is the quick and dirty approach, but not very general. Unfortunately, there is no way to get the names of the tables from the SP, which is probably what you want. One way to do that would be to modify your SP to return an output parameter:

CREATE PROCEDURE GetCustomers_Employees
   @tableNames varchar(20) OUTPUT
AS
BEGIN
    SET @tableNames = 'Customers,Employees'
    SELECT top 10 * from Customers
    SELECT top 10 * from Employees
END

But to make use of this, you also have to modify your SqlDataAdapter to handle a stored procedure with an output parameter:

using (SqlConnection = ...)
    {
       // sqlConnection.Open(); // Not really needed. Data Adapter will do this.

        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetCustomers_Employees";
        cmd.Connection = sqlConnection;

        // Create the parameter object and add it to the command
        SqlParameter param = new SqlParameter("@tableNames", SqlDbType.VarChar);
        param.Direction = ParameterDirection.Output;
        cmd.Parameters.Add(param);

        // Get the Data
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet(); 
        da.Fill(ds);

        // Set the names of the tables in the dataset
        string strTableNames = cmd.Parameters["@tableNames"].Value.ToString();
        string[] tableNames = strTableNames.split(',');

        for (int i=0; i<tableNames.Length; i++)
        {
            ds.Tables[i].TableName = tableNames[i];
        }
    }

Note that the above will handle any number of tables returned, so you could easily encapsulate this in a function, which you might find useful:

DataSet function(string storedProcedureName, string connectionString)
{
    DataSet ds = new DataSet();
    ... // code above, without DataSet declaration
    return ds;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文