繁体   English   中英

在C#中按名称读取多个#Temp表选择

[英]Reading multiple #Temp table select in C# by its name

我在SQL Server中有多个临时表,我想在一个查询中选择所有临时表; 像这样:

select * from #temp1
select * from #temp2
select * from #temp3

C#中是否有一种方法可以通过名称或别名来获取结果表? (不按索引)

编辑:所有表彼此不同。

select * from #temp1
union 
select * from #temp2
union
select * from #temp3

请记住,#temp1是全局临时表,仅在创建它的会话仍在运行或其他一些处理正在引用它时才存在。 这意味着在C#程序中,很有可能在您到达该程序时就消失了。

您正在寻找的是一个将存储所有返回表的数据集。 这是一个样本

 DataSet dsPrograms = new DataSet();
 SqlDataAdapter da = new SqlDataAdapter();
 SqlCommand cmdArcResponseHandler = new SqlCommand();
 SqlConnection conn = new SqlConnection(_dbConnection);
 if (conn.State == System.Data.ConnectionState.Closed)
 {
     conn.Open();
 }
 cmdArcResponseHandler.CommandText = @"select * from #temp1;
                                       select * from #temp2;
                                       select * from #temp3;"
 cmdArcResponseHandler.CommandTimeout = 120;
 cmdArcResponseHandler.CommandType = CommandType.Text;
 cmdArcResponseHandler.Connection = conn;
 da.SelectCommand = cmdArcResponseHandler;
 da.Fill(dsPrograms);
 ds.Tables[0].TableName = "Table 1";
 ds.Tables[0].TableName = "Table 2";
 ds.Tables[0].TableName = "Table 3";

希望这可以帮助..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM