簡體   English   中英

以編程方式使用c#刪除SQL Server 2016表

[英]Programmatically using c# drop a SQL Server 2016 table

我正在編寫的程序的一部分運行一些T-SQL代碼。 程序將繼續循環運行,直到我停止它。 當它第一次運行時沒有問題。 當它第二次運行時,我在嘗試運行T-SQL代碼時遇到錯誤。 它表示代碼創建的臨時表之一已存在於數據庫中。 代碼在嘗試將記錄插入臨時表之前,會刪除臨時表。 我不知道我是否在T-SQL代碼中正確配置了它,或者它是否無法運行丟棄表的T-SQL代碼(如果存在)。 我也嘗試將表格放在c#代碼中,這是我嘗試的內容:

cn.Open();
string cmdText = @"BEGIN TRANSACTION; DROP TABLE IF EXISTS #temp850; COMMIT TRANSACTION;";                
SqlCommand command = new SqlCommand(cmdText, cn);
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
cn.Close();

這是我收到的錯誤消息:

There is already an object named '#temp850' in the database.

有人可以幫忙嗎?

如果要在SQL中使用C#中的事務,則可以使用以下示例。

private static void ExecuteSqlTransaction(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        SqlCommand command = connection.CreateCommand();
        SqlTransaction transaction;

        // Start a local transaction.
        transaction = connection.BeginTransaction("SampleTransaction");

        // Must assign both transaction object and connection
        // to Command object for a pending local transaction
        command.Connection = connection;
        command.Transaction = transaction;

        try
        {
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.ExecuteNonQuery();
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
            command.ExecuteNonQuery();

            // Attempt to commit the transaction.
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
            Console.WriteLine("  Message: {0}", ex.Message);

            // Attempt to roll back the transaction.
            try
            {
                transaction.Rollback();
            }
            catch (Exception ex2)
            {
                // This catch block will handle any errors that may have occurred
                // on the server that would cause the rollback to fail, such as
                // a closed connection.
                Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                Console.WriteLine("  Message: {0}", ex2.Message);
            }
        }
    }
}

在創建臨時表之前,只需放置以下SQL語句即可

IF OBJECT_ID('tempdb..#temp850') IS NOT NULL DROP TABLE #temp850
GO

SELECT 1 col1 INTO #temp850

在sql上,您無法在同一查詢中多次創建相同的臨時表。

您可以在創建之前每次刪除#temp85表:

改變這一行:

string cmdText = @"BEGIN TRANSACTION; DROP TABLE IF EXISTS #temp850; COMMIT TRANSACTION;";       

**我認為這不是刪除臨時表的方法

為了這:

string cmdText = @"BEGIN TRANSACTION; if (OBJECT_ID('tempdb..#temp850')>0) drop table #temp850; COMMIT TRANSACTION;";        

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM