临时表如何在事务之前创建然后在事务内使用
我用 c# 编写代码。
我想在事务开始之前创建临时表,然后在事务中使用它,但是当我尝试这样做时,我在事务中收到错误,它表明“表不存在”。正确的做法是什么?
SqlConnection sqlConnection = new SqlConnection( "connstring" );
sqlConnection.Open();
string temp = string.Format( "CREATE TABLE dbo.#temp (id INT);" );
DbCommand command = database.GetSqlStringCommand( temp );
database.ExecuteNonQuery( command ); //here is the problem when I add argument , transaction it works
//fill data in temporary table
//...
// open transaction
SqlTransaction transaction = sqlConnection.BeginTransaction();
//Here I try to read from temp table I have some DbCommand readCommand
database.ExecuteNonQuery( readCommand, transaction );
I write code in c#.
I want to create temporary table before transaction begins and then use it within transaction, however when I try to do it I get error within transaction it estates that "Table does not exist". What is the proper way of doing it ?
SqlConnection sqlConnection = new SqlConnection( "connstring" );
sqlConnection.Open();
string temp = string.Format( "CREATE TABLE dbo.#temp (id INT);" );
DbCommand command = database.GetSqlStringCommand( temp );
database.ExecuteNonQuery( command ); //here is the problem when I add argument , transaction it works
//fill data in temporary table
//...
// open transaction
SqlTransaction transaction = sqlConnection.BeginTransaction();
//Here I try to read from temp table I have some DbCommand readCommand
database.ExecuteNonQuery( readCommand, transaction );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何仅对语句重新排序,以便在事务内进行表创建:
How about just re-ordering the statements as such, so that the table creation takes place inside the transaction:
下面的代码可以完美运行。
The following code work perfectly.