记录一个简单的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();
}
}
}
欢迎搜索公众号:雄雄的小课堂。