临时表如何在事务之前创建然后在事务内使用

发布于 2024-09-17 17:40:42 字数 680 浏览 9 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失去的东西太少 2024-09-24 17:40:42

如何仅对语句重新排序,以便在事务内进行表创建:

SqlConnection sqlConnection = new SqlConnection( "connstring" );
sqlConnection.Open();

// open transaction
SqlTransaction transaction = sqlConnection.BeginTransaction();

string temp = string.Format( "CREATE TABLE dbo.#temp (id INT);" );
DbCommand command = database.GetSqlStringCommand( temp );
database.ExecuteNonQuery( command, transaction  ); //here is the problem when I add argument , transaction it works

//Here I try to read from temp table I have some DbCommand readCommand
database.ExecuteNonQuery( readCommand, transaction ); 

How about just re-ordering the statements as such, so that the table creation takes place inside the transaction:

SqlConnection sqlConnection = new SqlConnection( "connstring" );
sqlConnection.Open();

// open transaction
SqlTransaction transaction = sqlConnection.BeginTransaction();

string temp = string.Format( "CREATE TABLE dbo.#temp (id INT);" );
DbCommand command = database.GetSqlStringCommand( temp );
database.ExecuteNonQuery( command, transaction  ); //here is the problem when I add argument , transaction it works

//Here I try to read from temp table I have some DbCommand readCommand
database.ExecuteNonQuery( readCommand, transaction ); 
黎夕旧梦 2024-09-24 17:40:42

下面的代码可以完美运行。

    static void Main(string[] args)
    {
        string conStr = "Integrated Security=true;Initial Catalog=sushma;server=(local)";

        SqlConnection sqlConnection = new SqlConnection(conStr);
        sqlConnection.Open();

        SqlCommand DbCommand = new SqlCommand("CREATE TABLE dbo.#temp (id INT);", sqlConnection);
        DbCommand.ExecuteNonQuery();

        SqlTransaction transaction = sqlConnection.BeginTransaction();
        DbCommand.CommandText = "SELECT * FROM dbo.#temp";
        DbCommand.Transaction = transaction;
        SqlDataReader dr = DbCommand.ExecuteReader();
        dr.Close();            

        transaction.Commit();
        Console.WriteLine("what is the issue");
        Console.ReadKey();
    }

The following code work perfectly.

    static void Main(string[] args)
    {
        string conStr = "Integrated Security=true;Initial Catalog=sushma;server=(local)";

        SqlConnection sqlConnection = new SqlConnection(conStr);
        sqlConnection.Open();

        SqlCommand DbCommand = new SqlCommand("CREATE TABLE dbo.#temp (id INT);", sqlConnection);
        DbCommand.ExecuteNonQuery();

        SqlTransaction transaction = sqlConnection.BeginTransaction();
        DbCommand.CommandText = "SELECT * FROM dbo.#temp";
        DbCommand.Transaction = transaction;
        SqlDataReader dr = DbCommand.ExecuteReader();
        dr.Close();            

        transaction.Commit();
        Console.WriteLine("what is the issue");
        Console.ReadKey();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文