C# 具有实际表名的数据集
我正在尝试将存储过程中的数据获取到数据集中。问题是,在dataset visualizer中,实际的表名即customers或employees并不仅仅显示在表1、表2等中。 有可能得到实际的表名吗C# 具有实际表名的数据集,c#,stored-procedures,dataset,C#,Stored Procedures,Dataset,我正在尝试将存储过程中的数据获取到数据集中。问题是,在dataset visualizer中,实际的表名即customers或employees并不仅仅显示在表1、表2等中。 有可能得到实际的表名吗 using (SqlConnection sqlConnection = new SqlConnection("Data Source=myserver;Initial Catalog=Northwind;Integrated Security=True")) {
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
您可以在执行填充操作时添加名称,如下所示:
da.Fill(ds, "MyTable");
da.Fill(ds);
ds.Tables[0].TableName = "Customers";
ds.Tables[1].TableName = "Employees";
从这一点开始,您可以参考以下表格:
ds.Tables["MyTable"];
而不是使用整数索引(即ds.Tables[0]
)
请看这里:
编辑:
在您的情况下,可以使用TableName
属性,如下所示:
da.Fill(ds, "MyTable");
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
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];
}
}
但要利用这一点,还必须修改SqlDataAdapter
,以处理带有输出参数的存储过程:
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
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];
}
}
@user603007请参阅答案中的其他信息。这有点痛苦,但我不认为有更简单的方法。