相关疑难解决方法(0)

将SqlConnection传递给类在dispose之后丢失其连接字符串

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

样品按预期进行

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我传递string给一个IDisposable类 …

.net c# sql-server

2
推荐指数
1
解决办法
1732
查看次数

标签 统计

.net ×1

c# ×1

sql-server ×1