Solved

Creating a Temp Table Programmatically

Posted on 2009-03-29
1
2,116 Views
Last Modified: 2012-05-06
I can create a temp table in Management Studio like this:

CREATE TABLE #ORDER_TEMP ([Order_ID] [int],[Customer_Number_FK] [nchar](10),[Product_ID_FK] [int])

However, using the same code from my C# app does not produce the same result.  Here's how I define the table and send the sql:

            sql = "CREATE TABLE #ORDER_TEMP (";
              sql = sql + " [Order_ID] [int] ";
            sql = sql + " ,[Customer_Number_FK] [nchar](10) ";
            sql = sql + " ,[Product_ID_FK] [int]) ";
            Program.ExecuteSQL(Program.wholesaleOrderingDatabase,sql);

            Program.InsertRecord(Program.wholesaleOrderingDatabase, "#ORDER_TEMP", "Order_ID,Customer_ID_FK,Product_ID_FK", "1,1," + iOrderNumber);


Here's the method that should build it:


    public static void ExecuteSQL(string connectionString, string sql)
    {
        //Executes whatever SQL string is passed in
        int recordsReturned;

        SqlConnection thisConnection = new SqlConnection(connectionString);
        thisConnection.Open();
        SqlCommand thisCommand = new SqlCommand(sql, thisConnection);
        recordsReturned = thisCommand.ExecuteNonQuery();
        thisConnection.Close();

    }


Why does this not work?  (I don't get an error message).
0
Comment
Question by:NigelRocks
1 Comment
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
Temp tables have a scope of a session. Each time you open a new connection (like you do), a new session is started with its own temp tables.

So you have some options:
-re-use the same connection instead of creating a new one each time.
-use global temp tables (##ORDER_TEMP)
-Don't use temp tables
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now