views:

46

answers:

2

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 ); 
+1  A: 

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 ); 
Peter Bridger
I heard that it might be harmful for performance : as in (http://www.sql-server-performance.com/tips/temp_table_tuning_p1.aspx ) "If you have to use a temp table, do not create it from within a transaction. If you do, then it will lock some system tables (syscolumns, sysindexes, and syscomments) and prevent others from executing the same query, greatly hurting concurrency and performance. In effect, this turns your application into a single-user application."
Darqer
@Darqer You may be able to create an in-memory temp table: `DECLARE TABLE @Temp (id INT);` which shouldn't have any of those side effects
Peter Bridger
I tried but then I have to do everything within one bath and it is not possible.
Darqer
+1  A: 

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();
    }
Guru