路过的SqlConnection类处置后,失去了它的ConnectionString它的、失去了、SqlConnection、ConnectionString

2023-09-05 03:19:17 作者:念旧的人总像个拾荒者

我有以下简单的类定义(一个Windows控制台应用程序):

I have following simple class definition (a windows console application):

样品按预期

using System;

namespace ConsoleApplication1 {

    class Test: IDisposable {
        string Text;

        public Test(string str) {
            Text = str;
        }

        public void print() {
            Console.WriteLine("Text: " + Text);
        }

        public void Dispose() {
            Text = "";
        }
    }

    class Program {
        static void Main(string[] args) {
            string TeXT = "Data Source=localhost;Initial Catalog=master;";

            using (Test test = new Test(TeXT)) {
                test.print();
            }

            using (Test test = new Test(TeXT)) {
                test.print();
            }

            Console.ReadKey();
        }
    }
}

在上面的例子中我通过字符串的IDisposable 测试。只要code超出范围则其设置。正如所预期的局部变量文本处置测试后第一次没有被触及,这是测试类的第二个实例相同的方式,它是可访问的。

in above example I pass a string to a IDisposable class test. As soon as code goes out of scope it is disposed. As expected the local variable TeXT is not touched after disposing the test for the first time and it is accessible for second instance of test class the same way it was.

现在这里是让我抓我的头几个小时的真实场景。

Now here is the real scenario that made me scratch my head for hours.

实际结果

using System;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1 {

    class Test: IDisposable{
        SqlConnection dbConn;

        public Test(SqlConnection connection){
            dbConn = connection;
        }

        public void print(){
            Console.WriteLine("Connection" + dbConn.ConnectionString);
        }

        public void Dispose() {
            dbConn.Dispose();
        }
    }

    class Program {
        static void Main(string[] args) {
            SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=master;");

            using(Test test = new Test(sqlConn)){
                test.print();
            }

            using(Test test = new Test(sqlConn)){
                test.print();
            }

            Console.ReadKey();
        }
    }
}

和以前一样我通过的SqlConnection 测试类的一个实例。配置第一个实例之后,的SqlConnection 失去的连接字符串。这使得类的一个实例是不可用的。

Same as before I pass a SqlConnection to first instance of test class. After disposing the first instance, SqlConnection looses the connection string. which makes it unavailable for next instance of class.

问题 为什么在第二种情况下我失去的ConnectionString?这是一个错误还是有意为之?有没有什么解决方案,以避免这种情况,除了制作到数据库服务器的多个连接?我应该停止处理的内部连接实例?

Question Why in second case I lose ConnectionString? Is this a bug or intentional? Is there any solution to avoid this except making multiple connections to DB Server? should I stop disposing the internal connection instance?

注意 SqlCommand-SqlConnection利用处置问题没有回答我的问题。

Note SqlCommand-SqlConnection Using Disposing issue does not answer my question

推荐答案

您正在处理的 SqlConection 在废弃测试 - 但你随后试图再次使用它

You're disposing of the SqlConection when you dispose of the Test - but you're then trying to use it again.

最好的办法是几乎总是的创建一个新的的SqlConnection 每次需要时间 - 并处理它,只要你是完成它。你已经在做第二部分,但你应该为第二个操作创建一个新的的SqlConnection 实例。

The best approach is almost always to create a new SqlConnection each time you need one - and dispose of it as soon as you're finished with it. You're already doing the second part, but you should create a new SqlConnection instance for the second operation.

请注意的不的意思是让到服务器多个连接,必然 - 在.NET连接池会处理多少实际的网络连接都需要

Note that that doesn't mean making multiple connections to the server, necessarily - the .NET connection pool will handle how many actual network connections are needed.