English 中文(简体)
• 宣布非银在静态真空中的联系——连接库是否继续存在?
原标题:Declaring DB connection in static void - will the connection pool persist?

我有一台多读服务器,使用每个客户都应能够使用的一台中高校车。 我也想利用联通:

public static void DBconn()
        {
                SqlConnection pripojeni = new SqlConnection();
                pripojeni.ConnectionString = "Data Source=localhost\SQLEXPRESS;Initial Catalog=XYZ;Trusted_Connection=True;Min Pool Size=20";

        }

如果连接显示国家“人才库”的话,目标是否将永远留在记忆中(不是目标本身,而是开放的链接)。 或者,在完成这一方法之后,它会走吗? 增 编

最佳回答

您应使用新的<代码>SqlConnection,对每一链接使用,而不是在固定构造中制造。 每次连接时,联谊会都使用同样的连接线铺设(应当将其存放在像网络、汇合或 app等配置文件中)。

仅创建和开放>,每次都标出,记住最终关闭/处置该编码,连接库将确保该编码重新使用现有的开放式连接。

希望帮助!

问题回答

由于你在这种方法中新宣布了SqlConnection标的,它将在方法结束之后进行。

争取继续联系:

public static class PerstistendDB
    {
        // Readonly so it can t be destroyed!
        public static readonly System.Data.SqlClient.SqlConnection pripojeni = new System.Data.SqlClient.SqlConnection(
            "Data Source=localhost\SQLEXPRESS;Initial Catalog=XYZ;Trusted_Connection=True;Min Pool Size=20");
    }

注:我同意Kieren的看法,如果你随时开放你的联系,你将 database清数据库,(几乎总是)更愿意开放和关闭。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签