C# 连接变为空
我正在开发一个类来管理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,它是 null,因为您从未真正初始化
Connection
属性,并且在您初始化它之前它将为 null。因此,不要:初始化属性:
虽然这可能会修复 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:initialize the property:
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.在构造函数中,您没有设置属性 Connection。您正在声明一个局部变量 Connection。因此,您从未初始化过 Connection 属性。
更正代码,使用:
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:
因为您在这里将 Connection 声明为局部变量:
尝试
Because you declared Connection as a local variable here:
Try
将您的构造函数更改为:
Change your constructor to:
在 MysqlAccess 构造函数中,您分配给一个新的局部变量:
...然后您引用一个属性,该属性恰好与局部变量具有相同的名称:
更改 MySqlConnection 连接 到构造函数中的
Connection
应该可以修复它。In the
MysqlAccess
constructor you assign to a new local variable:...then later on you refer to a property, which happens to have the same name as the local variable:
Changing the
MySqlConnection Connection
to justConnection
in the constructor should fix it.