几个C#数据库封装类
由于SqlDataReader需要一个长效连接的原因,把它从CMySqlConnection中独立了出来,感觉有些松散,另外也有点重复封装的味道,不过实际使用中方便了许多,不用经常打开关闭数据库,省了不少。
class CMySqlConnection //SqlConnection封装类
{
private SqlConnection m_objConn;
//基于连接字符串构建对象
public CMySqlConnection(string strConnString)
{
m_objConn = new SqlConnection(strConnString);
}
//打开数据库连接
private void Open()
{
Close();
m_objConn.Open();
}
//关闭数据库连接
private void Close()
{
if(m_objConn.State == ConnectionState.Open)
m_objConn.Close();
}
//执行Sql语句(不返回记录集的)
private int ExecSql(string strSql)
{
int nRet = 0;
try
{
this.Open();
SqlCommand objSqlCommand = new SqlCommand(strSql, m_objConn);
nRet = objSqlCommand.ExecuteNonQuery();
this.Close();
}
catch (Exception objException)
{
MessageBox.Show("执行SQL语句时出错,出错信息为:" + objException.ToString(), "错误");
}
return nRet; //返回SQL语句执行后所影响的行数
}
//执行一个查询操作
public DataSet doSelect(string strSql,string strTableName)
{
DataSet objDataSet = null;
SqlDataAdapter objDataAdapter = null;
try
{
this.Open();
objDataSet = new DataSet();
objDataAdapter = new SqlDataAdapter(strSql, m_objConn);
objDataAdapter.Fill(objDataSet, strTableName);
this.Close();
}
catch (Exception objException)
{
MessageBox.Show("执行SQL语句时出错,出错信息为:" + objException.ToString(), "错误");
}
return objDataSet;
}
//执行一个插入操作
public int doInsert(string strSql)
{
return ExecSql(strSql);
}
//执行一个更新操作
public int doUpdate(string strSql)
{
return ExecSql(strSql);
}
//执行一个删除操作
public int doDelete(string strSql)
{
return ExecSql(strSql);
}
}
{
private SqlConnection m_objConn;
//基于连接字符串构建对象
public CMySqlConnection(string strConnString)
{
m_objConn = new SqlConnection(strConnString);
}
//打开数据库连接
private void Open()
{
Close();
m_objConn.Open();
}
//关闭数据库连接
private void Close()
{
if(m_objConn.State == ConnectionState.Open)
m_objConn.Close();
}
//执行Sql语句(不返回记录集的)
private int ExecSql(string strSql)
{
int nRet = 0;
try
{
this.Open();
SqlCommand objSqlCommand = new SqlCommand(strSql, m_objConn);
nRet = objSqlCommand.ExecuteNonQuery();
this.Close();
}
catch (Exception objException)
{
MessageBox.Show("执行SQL语句时出错,出错信息为:" + objException.ToString(), "错误");
}
return nRet; //返回SQL语句执行后所影响的行数
}
//执行一个查询操作
public DataSet doSelect(string strSql,string strTableName)
{
DataSet objDataSet = null;
SqlDataAdapter objDataAdapter = null;
try
{
this.Open();
objDataSet = new DataSet();
objDataAdapter = new SqlDataAdapter(strSql, m_objConn);
objDataAdapter.Fill(objDataSet, strTableName);
this.Close();
}
catch (Exception objException)
{
MessageBox.Show("执行SQL语句时出错,出错信息为:" + objException.ToString(), "错误");
}
return objDataSet;
}
//执行一个插入操作
public int doInsert(string strSql)
{
return ExecSql(strSql);
}
//执行一个更新操作
public int doUpdate(string strSql)
{
return ExecSql(strSql);
}
//执行一个删除操作
public int doDelete(string strSql)
{
return ExecSql(strSql);
}
}
class CMySqlDataReader //SqlDataReader封装类
{
private bool m_bIsSelfConn; //是否新建数据库连接标志
private SqlConnection m_objConn; //数据库连接对象
private SqlDataReader m_objSqlDataReader; //阅读器对象
//新建一个数据库连接构建对象
public CMySqlDataReader(string strConnString)
{
m_objConn = new SqlConnection(strConnString);
try
{
m_objConn.Open();
m_bIsSelfConn = true;
}
catch (Exception objException)
{
MessageBox.Show("打开数据库连接时出错,出错信息为:" + objException.ToString(), "错误");
}
}
//基于一个现存的数据库连接对象(必须已打开)构建对象
public CMySqlDataReader(SqlConnection objConn)
{
if (objConn.State == ConnectionState.Open)
{
m_objConn = objConn;
m_bIsSelfConn = false;
}
else
{
throw new Exception("数据库连接未打开!"); //如果连接未打开则抛出异常
}
}
//打开并获得一个阅读器
public SqlDataReader Open(string strSql)
{
SqlCommand objSqlCommand = null;
m_objSqlDataReader = null;
try
{
objSqlCommand = new SqlCommand(strSql, m_objConn);
m_objSqlDataReader = objSqlCommand.ExecuteReader();
}
catch (Exception objException)
{
MessageBox.Show("创建一个SqlDataReader时出错,出错信息为:" + objException.ToString(), "错误");
}
return m_objSqlDataReader;
}
//关闭一个阅读器
public void Close()
{
m_objSqlDataReader.Close();
if (m_bIsSelfConn) //如果数据库连接为新建的,则关闭它
{
if (m_objConn.State == ConnectionState.Open)
m_objConn.Close();
}
}
}
{
private bool m_bIsSelfConn; //是否新建数据库连接标志
private SqlConnection m_objConn; //数据库连接对象
private SqlDataReader m_objSqlDataReader; //阅读器对象
//新建一个数据库连接构建对象
public CMySqlDataReader(string strConnString)
{
m_objConn = new SqlConnection(strConnString);
try
{
m_objConn.Open();
m_bIsSelfConn = true;
}
catch (Exception objException)
{
MessageBox.Show("打开数据库连接时出错,出错信息为:" + objException.ToString(), "错误");
}
}
//基于一个现存的数据库连接对象(必须已打开)构建对象
public CMySqlDataReader(SqlConnection objConn)
{
if (objConn.State == ConnectionState.Open)
{
m_objConn = objConn;
m_bIsSelfConn = false;
}
else
{
throw new Exception("数据库连接未打开!"); //如果连接未打开则抛出异常
}
}
//打开并获得一个阅读器
public SqlDataReader Open(string strSql)
{
SqlCommand objSqlCommand = null;
m_objSqlDataReader = null;
try
{
objSqlCommand = new SqlCommand(strSql, m_objConn);
m_objSqlDataReader = objSqlCommand.ExecuteReader();
}
catch (Exception objException)
{
MessageBox.Show("创建一个SqlDataReader时出错,出错信息为:" + objException.ToString(), "错误");
}
return m_objSqlDataReader;
}
//关闭一个阅读器
public void Close()
{
m_objSqlDataReader.Close();
if (m_bIsSelfConn) //如果数据库连接为新建的,则关闭它
{
if (m_objConn.State == ConnectionState.Open)
m_objConn.Close();
}
}
}