C#连接变为空
我正在开发一个类来管理Mysql数据库上的操作。 我有以下代码:C#连接变为空,c#,asp.net,mysql,null,connection,C#,Asp.net,Mysql,Null,Connection,我正在开发一个类来管理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() {
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时,连接是空的,为什么
提前谢谢 嗯,它是空的,因为您从未真正初始化
连接
属性,并且在您初始化它之前它将是空的。因此,不是:
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);
}
虽然这可能会解决您所遇到的问题,但您应该知道,
MySqlConnection
实现意味着您应该确保调用该方法,以避免在每个请求上泄漏连接或创建新连接,这在web应用程序中可能会造成灾难性后果。在构造函数中,您没有设置属性连接。您正在声明一个局部变量连接。因此,您从未初始化属性连接
请使用以下命令更正代码:
this.Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
因为您在此处将连接声明为局部变量:
public MysqlAccess()
{
MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
}
试一试
将构造函数更改为:
public MysqlAccess()
{
Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
}
在
MysqlAccess
构造函数中,您分配给一个新的局部变量:
MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
Response.Write("Connection State:" + DBManager.Connection.State.ToString());
…然后,稍后您将引用一个属性,该属性恰好与局部变量同名:
MySqlConnection Connection=new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
Response.Write("Connection State:" + DBManager.Connection.State.ToString());
将构造函数中的
MySqlConnection连接
更改为仅连接
,应该可以解决此问题。感谢大家的帮助。似乎我对连接词=)有点混淆了,谢谢!