Sui*_*eep 2 c# sql temp-tables
基于SQL临时表的教程,应该可以通过使用创建临时表,SELECT * INTO #tempTable FROM tableA但是SQLException当我试图SELECT * FROM #tempTable说出来时它会抛弃我Invalid object name '#tempTable'.我可以知道使用临时表的正确方法是C#什么?
string sql = "SELECT * INTO ##tempTable FROM (SELECT * FROM tableA)";
using (var command = new SqlCommand(sql, connection))
{
string sqlNew = "SELECT * FROM ##tempTable";
using (var command2 = new SqlCommand(sqlNew, connection))
{
using (var reader = command2.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader["column1"].ToString());
}
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标是尝试使用从中检索的数据sqlVar并将它们插入到tempTable中并对其执行一些操作.非常感谢如果有一些示例代码如何使代码适合上述代码.谢谢.
但是为什么你需要SQL服务器端的临时表..
1)如果你想在C#端执行操作,只需要输入数据DATASET而不是DATAREADER..和
DataSet dataset = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("select * from tableA", conn);
conn.Open();
adapter.Fill(dataset);
conn.Close();
foreach (DataRow row in dataset.Tables[0]) // Loop over the rows.
{
// perform your operation
}
}
Run Code Online (Sandbox Code Playgroud)
2)如果你需要在SQL端执行操作,那么在中创建一个stored procedureSQL服务器.. stored procedure create #table并使用它..
3)并且您不想创建DATASET然后您可以LIST在C#端获取数据并执行操作