簡體   English   中英

等待創建數據庫完成

[英]Wait for creation database to finish

我有這樣的代碼:

using (var sqlConnection = new SqlConnection(connectionString))
{
  sqlConnection.Open();
  using (var sqlCommand = new SqlCommand(scriptText, sqlConnection))
  {
    sqlCommand.ExecuteNonQuery();
  }
}

我第一次調用它來創建一個數據庫,然后我形成一個連接字符串到這個數據庫,並嘗試做一些工作( Open工作正常,並表示連接實際上是為第二個連接打開)。 但我得到一個“現有連接被遠程主機強行關閉”,我可以看到sql日志“無法找到數據庫ID 9.數據庫可能尚未激活或可能正在轉換”。 所以,實際上ExecuteNonQuery在作業完成之前返回。 當然,我可以直接垃圾郵件直到情況發生變化,但有沒有更好的方法等待數據庫准備工作?

這個完整的樣本適合我。

它創建數據庫,創建表,在表中放置一行。

不知道你的結果有什么問題。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace GranadaCoder.PlaygroundConsole.SqlStuff
{
    public class SqlPlayground
    {

        public static void EntryPointStuff()
        {
            string databaseName = "MyFirstDatabaseABC";
            string connectionString = string.Empty;
            string commandText = string.Empty;
            int returnValue = 0;
            string msg = string.Empty;

            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder["Initial Catalog"] = "master";
            builder["integrated Security"] = true;
            builder["Server"] = @"MyMachine\MyInstance";
            connectionString = builder.ConnectionString;


            commandText = string.Format("IF EXISTS (Select * from sys.databases where name = N'{0}') DROP DATABASE [{0}]", databaseName);
            returnValue = RunACommand(connectionString, commandText);
            msg = string.Format("'{0}', {1}", returnValue, commandText);
            Console.WriteLine(msg);

            commandText = string.Format("IF NOT EXISTS (Select * from sys.databases where name = N'{0}') CREATE DATABASE [{0}]", databaseName);
            returnValue = RunACommand(connectionString, commandText);
            msg = string.Format("'{0}', {1}", returnValue, commandText);
            Console.WriteLine(msg);

            /* Change the Catalog */
            builder["Initial Catalog"] = databaseName;
            connectionString = builder.ConnectionString;


            commandText = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CodeCategory]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)    BEGIN       DROP TABLE [dbo].[CodeCategory]     END ";
            returnValue = RunACommand(connectionString, commandText);
            msg = string.Format("'{0}', {1}", returnValue, commandText);
            Console.WriteLine(msg);

            commandText = "CREATE TABLE [dbo].[CodeCategory] (    CodeCategoryKey           [smallint] not null     , CodeCategoryName      varchar(64) not null  ) ";
            returnValue = RunACommand(connectionString, commandText);
            msg = string.Format("'{0}', {1}", returnValue, commandText);
            Console.WriteLine(msg);


            commandText = "INSERT INTO [dbo].[CodeCategory] (     CodeCategoryKey       , CodeCategoryName    ) Select 1001 , 'MyFirstCodeCategory' ";
            returnValue = RunACommand(connectionString, commandText);
            msg = string.Format("'{0}', {1}", returnValue, commandText);
            Console.WriteLine(msg);

            commandText = "Select Count(*) from [dbo].[CodeCategory]";
            returnValue = RunACommand(connectionString, commandText);
            msg = string.Format("'{0}', {1}", returnValue, commandText);
            Console.WriteLine(msg);

        }




        private static int RunACommand(string connectionString, string scriptText)
        {

            int returnValue = 0;

            using (var sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();
                using (var sqlCommand = new SqlCommand(scriptText, sqlConnection))
                {
                    returnValue = sqlCommand.ExecuteNonQuery();
                }
                sqlConnection.Close();
            }

            return returnValue;
        }






    }

}

這對我有用(沒有例外):

    static void Main(string[] args)
    {

        try
        {

            SqlPlayground.EntryPointStuff();

            Console.WriteLine("Press Enter after deleting the database in SSMS manually with 'Close Existing Connections' checked.");
            Console.ReadLine();

            SqlPlayground.EntryPointStuff();


        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }


        Console.WriteLine("Pressing Enter To End");
        Console.ReadLine();

    }

在我的情況下添加Thread.Sleep(...)幫助,但根本原因是失敗的連接被緩存並由方法SqlConnection.Open()使用。 針對這種情況,請參閱此問題的答案 使用SqlConnection.ClearAllPools()清除池有幫助。

為什么等待幾秒鍾也有幫助呢? 似乎通過計時器自動清除池就足夠了。

如果您在創建后立即執行其他操作(例如更新數據庫架構),則可能會注意到此問題。 等待幾秒似乎解決了這個問題。

//wait for database creation
Thread.Sleep(3000);

暫無
暫無

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

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