很简单很简单的DBHelper类
程序员文章站
2022-03-12 12:42:37
记录一个简单的DBHelper类吧,用的时候在上来拿!/// /// 数据库连接工具类 /// public class DBHelper { string constr = "Data Source=.;Initial Catalog=schoolDB;Integrated Security=True"; private SqlConnection conn; ....
记录一个简单的DBHelper类吧,用的时候在上来拿!
/// <summary>
/// 数据库连接工具类
/// </summary>
public class DBHelper
{
string constr = "Data Source=.;Initial Catalog=schoolDB;Integrated Security=True";
private SqlConnection conn;
//数据库连接Connection对象
public SqlConnection getConnection() {
if(conn==null){
conn = new SqlConnection(constr);
}
return conn;
}
//打开连接
public void OpenConnection() {
if(conn.State==ConnectionState.Closed){
conn.Open();
}else if(conn.State==ConnectionState.Broken){
conn.Close();
conn.Open();
}
}
//关闭连接
public void CloseConnection() {
if(conn.State==ConnectionState.Open||conn.State==ConnectionState.Broken){
conn.Close();
}
}
}
欢迎搜索公众号:雄雄的小课堂。
本文地址:https://blog.csdn.net/qq_34137397/article/details/107394236