加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 调用SMO服务器和数据库后清理

发布时间:2020-12-16 06:43:36 所属栏目:asp.Net 来源:网络整理
导读:你如何让SMO发布它的连接? 我有这个代码: public static class SqlServerConnectionFactory{ public static Server GetSmoServer() { using (var c = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
你如何让SMO发布它的连接?

我有这个代码:

public static class SqlServerConnectionFactory
{
    public static Server GetSmoServer()
    {
        using (var c = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
        {
            var s = new ServerConnection(c);
            c.Close();
            return new Server(s);
        }
    }

    public static Database GetSmoDatabase(Server server)
    {
        var db = server.Databases[ConfigurationManager.AppSettings["Database"]];
        db.AutoClose = true;
        return db;
    }
}

从IIS中运行的ASP.Net MVC应用程序调用这样的:

public ActionResult Index()
    {
        server = SqlServerConnectionFactory.GetSmoServer();
        database = SqlServerConnectionFactory.GetSmoDatabase(server);
        var vm = new SettingsIndexViewmodel(database);
        return View(vm);
    }

对于我对此索引方法进行的每次调用,都会启动连接 – 并且不会再次释放.

所以在20次调用页面后,我有20个等待命令的连接.当我无法建立新连接时,最终会出现异常,因为连接池已满.

我需要做些什么来避免这种情况发生?我似乎无法在SMO Server对象上找到像Dispose,close或类似的方法.

解决方法

MSDN文章 Disconnecting from an Instance of SQL Server可能会提供一些帮助.它指出:

When the 07001 method is called,the connection is not automatically released. The 07002 method must be called explicitly to release the connection to the connection pool. Also,you can request a non-pooled connection. You do this by setting the 07003 property of the 07004 property that references the 07005 object

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读