如何命名在存储过程中返回的数据集的表?

2024-04-18

我有以下存储过程

Create procedure psfoo ()
AS
select * from tbA
select * from tbB

然后我以这种方式访问​​数据:

     Sql Command mySqlCommand = new SqlCommand("psfoo" , DbConnection)
     DataSet ds = new DataSet();
     mySqlCommand.CommandType = CommandType.StoredProcedure;
     SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
     mySqlDataAdapter.SelectCommand = mySqlCommand;
     mySqlDataAdapter.Fill(ds);

现在,当我想访问我的表时,我必须这样做:

     DataTable datatableA = ds.Tables[0];
     DataTable datatableB = ds.Tables[1];

数据集 Tables 属性还通过字符串(而不是 int)获得了访问器。

是否可以在 SQL 代码中指定表的名称,以便我可以这样写:

     DataTable datatableA = ds.Tables["NametbA"];
     DataTable datatableB = ds.Tables["NametbB"];

我正在使用 SQL Server 2008,如果这有什么区别的话。


据我所知,从存储过程来看,你不能这样做。但是,您可以在检索数据集后设置名称,然后从那时起使用它们。

ds.Tables[0].TableName = "NametbA";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何命名在存储过程中返回的数据集的表? 的相关文章

随机推荐