C# 连接变为空

发布于 2024-09-09 07:25:24 字数 1237 浏览 3 评论 0原文

我正在开发一个类来管理 Mysql 数据库上的操作。 我有以下代码:

 using System;
using MySql.Data.MySqlClient;

public class MysqlAccess
{
    private MySqlConnection pCnn;
    public enum OperationType {Select = 1,Insert = 2,Update = 3,Delete = 4};

    public MysqlAccess()
    {

        MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);


    }

   public MySqlConnection Connection { get {return pCnn;} set {pCnn=value;} }


    public int Connect()
    {
        try
        {

            Connection.Open();

            if (Connection.State == System.Data.ConnectionState.Open)
            {
                return 1;
            }
            else
            {
                return 0;
            }

        }
        catch (Exception e)
        {
            return -1;

        }
    }



    }

}

页面加载代码

 protected void Page_Load(object sender, EventArgs e)
    {

        MysqlAccess DBManager = new MysqlAccess();


        DBManager.Connect();
        Response.Write("Connection State:" + DBManager.Connection.State.ToString());
    }

当我执行response.write 时,连接为空,为什么?

提前致谢!

I'm developing a class to manage the operations on a Mysql database.
I have the following code:

 using System;
using MySql.Data.MySqlClient;

public class MysqlAccess
{
    private MySqlConnection pCnn;
    public enum OperationType {Select = 1,Insert = 2,Update = 3,Delete = 4};

    public MysqlAccess()
    {

        MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);


    }

   public MySqlConnection Connection { get {return pCnn;} set {pCnn=value;} }


    public int Connect()
    {
        try
        {

            Connection.Open();

            if (Connection.State == System.Data.ConnectionState.Open)
            {
                return 1;
            }
            else
            {
                return 0;
            }

        }
        catch (Exception e)
        {
            return -1;

        }
    }



    }

}

Page Load Code

 protected void Page_Load(object sender, EventArgs e)
    {

        MysqlAccess DBManager = new MysqlAccess();


        DBManager.Connect();
        Response.Write("Connection State:" + DBManager.Connection.State.ToString());
    }

When i do the response.write the connection is null, why?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆梦 2024-09-16 07:25:25

好吧,它是 null,因为您从未真正初始化 Connection 属性,并且在您初始化它之前它将为 null。因此,不要:

public MysqlAccess()
{
    // Here you are initializing a local variable 
    // that is subject to GC and goes into the void of forgetfulness
    MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
}

初始化属性:

public MysqlAccess()
{
    var connectionString = ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString;
    // The 'this' keyword is optional here and its usage is a 
    // matter of personal preference
    this.Connection = new MySqlConnection(connectionString);
}

虽然这可能会修复 NullReferenceException 你应该知道 MySqlConnection 实现 IDisposable 意味着您应该确保调用 Dispose 方法以避免泄漏连接或在每个请求上创建新连接,这在 Web 应用程序中可能会造成特别灾难性的后果。

Well, it is null because you never really initialize the Connection property and it will be null until you initialize it. So instead of:

public MysqlAccess()
{
    // Here you are initializing a local variable 
    // that is subject to GC and goes into the void of forgetfulness
    MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
}

initialize the property:

public MysqlAccess()
{
    var connectionString = ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString;
    // The 'this' keyword is optional here and its usage is a 
    // matter of personal preference
    this.Connection = new MySqlConnection(connectionString);
}

While this might fix the NullReferenceException you are getting you should be aware that MySqlConnection implements IDisposable meaning that you should make sure to call the Dispose method to avoid leaking connections or creating new connections on each request which could be particularly catastrophic in a web application.

烟雨凡馨 2024-09-16 07:25:25

在构造函数中,您没有设置属性 Connection。您正在声明一个局部变量 Connection。因此,您从未初始化过 Connection 属性。

更正代码,使用:

this.Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);

In the constructor, you are not setting the property Connection. You're are declaring a local variable Connection. So, you've never initializes the property Connection.

Correct the code, using:

this.Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
夏夜暖风 2024-09-16 07:25:25

因为您在这里将 Connection 声明为局部变量:

public MysqlAccess()
{

    MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);


}

尝试

public MysqlAccess()
{

    Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);


}

Because you declared Connection as a local variable here:

public MysqlAccess()
{

    MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);


}

Try

public MysqlAccess()
{

    Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);


}
柠檬 2024-09-16 07:25:25

将您的构造函数更改为:

public MysqlAccess()
{
    Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
}

Change your constructor to:

public MysqlAccess()
{
    Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
}
狼亦尘 2024-09-16 07:25:25

在 MysqlAccess 构造函数中,您分配给一个新的局部变量:

MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);

...然后您引用一个属性,该属性恰好与局部变量具有相同的名称:

Response.Write("Connection State:" + DBManager.Connection.State.ToString());

更改 MySqlConnection 连接 到构造函数中的 Connection 应该可以修复它。

In the MysqlAccess constructor you assign to a new local variable:

MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);

...then later on you refer to a property, which happens to have the same name as the local variable:

Response.Write("Connection State:" + DBManager.Connection.State.ToString());

Changing the MySqlConnection Connection to just Connection in the constructor should fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文