具有实际表名的数据集
我正在尝试将数据从存储过程获取到数据集中。问题是,在数据集可视化工具中,实际的表名称(即客户或员工)不仅仅显示 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在执行填充操作时添加名称,如下所示:
从那时起,您可以将表引用为
而不是使用整数索引(即
ds.Tables[0]
)请参阅这里:http://msdn.microsoft.com/en-us/library/bh8kx08z(v=VS.100).aspx
编辑:
在您的情况下,您可以使用
TableName
属性,如下所示:这是一种快速而肮脏的方法,但不是很通用。不幸的是,无法从 SP 获取表的名称,而这可能正是您想要的。一种方法是修改 SP 以返回输出参数:
但是要利用此功能,您还必须修改
SqlDataAdapter
以处理带有输出参数的存储过程:请注意上面的代码将处理返回的任意数量的表,因此您可以轻松地将其封装在一个函数中,您可能会发现该函数很有用:
You can add a name when you do the fill operatation, like this:
From that point forward, you can refer to the table as
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: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:
But to make use of this, you also have to modify your
SqlDataAdapter
to handle a stored procedure with an output parameter: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: