代码之家  ›  专栏  ›  技术社区  ›  Claudi

如何强制SQLconnection释放数据库?

  •  0
  • Claudi  · 技术社区  · 17 年前

    我编写了一个小型应用程序,可以还原数据库(C#和SQL2005),但在访问数据库后,我无法删除它——它说它正在使用中。。 我想这与SQL连接池有关,但我可以强制它重新建立数据库吗??

    5 回复  |  直到 17 年前
        1
  •  2
  •   leppie    17 年前

    在连接字符串中指定“Pooling=False”。

        2
  •  1
  •   Fabian Vilers    17 年前

    处理SqlConnection对象。

        3
  •  0
  •   Dean    17 年前

    如果你使用

    Using SQLConnection
        '' do your stuff here
    End Using
    

        4
  •  0
  •   stuartd saeed    17 年前

    呼叫“USE SomeOtherDB”(例如Master)关闭您自己的连接,或

    使用ROLLBACK_IMMEDIATE更改数据库设置singleuser

    关闭其他连接。第一个等待连接结束,第二个是即时的。

        5
  •  0
  •   Dan    13 年前

    呼叫“USE SomeOtherDB”(例如Master)关闭您自己的连接,或

    //on master ... CREATE
    using (var cnn = new SqlConnection(MASTER))
    {
        cnn.Open();
        var createDbCmd = new SqlCommand(string.Format("create database [{0}]", db), cnn).ExecuteNonQuery();
        cnn.Close();
    }
    using (var cnn = new SqlConnection(tempDB))
    {
        cnn.Open();
        var createTbl = new SqlCommand(string.Format("create table t (t int )"), cnn).ExecuteNonQuery();
        var dropTbl = new SqlCommand(string.Format("drop table t"), cnn).ExecuteNonQuery();
        //Do additional stuf
        var userMaster = new SqlCommand(string.Format("use master"), cnn).ExecuteNonQuery();
        cnn.Close();
    
    }
    //on master ... CREATE
    using (var cnn = new SqlConnection(MASTER))
    {
        cnn.Open();
        var dropDbCmd = new SqlCommand(string.Format("drop database [{0}]", db), cnn).ExecuteNonQuery();
        cnn.Close();
    
    }