作者:刘小飘168585 | 来源:互联网 | 2022-01-22 03:09
本文模仿实现数据库访问的通用类,代码清晰,而且很实用,包括了对数据库的所有的常用的操作。
///
/// 数据库访问通用类
///
public class SqlHelper
{
private string connectionString;
///
/// 设定数据库访问字符串
///
public string ConnectionString
{
set { cOnnectionString= value; }
}
///
/// 构造函数
///
/// 数据库访问字符串
public SqlHelper(string connectionString)
{
this.cOnnectionString= connectionString;
}
///
/// 执行一个查询,并返回查询结果
///
/// 要执行的sql语句
/// 要执行的查询语句的类型,如存储过程或者sql文本命令
/// 返回查询结果集
public DataTable ExecuteDataTable(string sql,CommandType commandType)
{
return ExecuteDataTable(sql, commandType, null);
}
///
/// 执行一个查询,并返回结果集
///
/// 要执行的sql文本命令
/// 返回查询的结果集
public DataTable ExecuteDataTable(string sql)
{
return ExecuteDataTable(sql, CommandType.Text, null);
}
///
/// 执行一个查询,并返回查询结果
///
/// 要执行的sql语句
/// 要执行查询语句的类型,如存储过程或者sql文本命令
/// Transact-SQL语句或者存储过程参数数组
///
public DataTable ExecuteDataTable(string sql, CommandType commandtype, SqlParameter[] parameters)
{
DataTable data = new DataTable(); //实例化datatable,用于装载查询结果集
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.CommandType = commandtype;//设置command的commandType为指定的Commandtype
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (SqlParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
}
//通过包含查询sql的sqlcommand实例来实例化sqldataadapter
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(data);//填充datatable
}
}
return data;
}
///
/// 返回一个SqlDataReader对象的实例
///
/// 要执行的SQl查询命令
///
public SqlDataReader ExecuteReader(string sql)
{
return ExecuteReader(sql, CommandType.Text, null);
}
///
///
///
/// 要执行的sql语句
/// 要执行查询语句的类型,如存储过程或者SQl文本命令
///
public SqlDataReader ExecuteReader(string sql,CommandType commandType)
{
return ExecuteReader(sql, commandType, null);
}
///
/// 返回一个sqldatareader对象的实例
///
///
///
///
///
public SqlDataReader ExecuteReader(string sql, CommandType commandType, SqlParameter[] parameters)
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(sql, con);
if (parameters != null)
{
foreach (SqlParameter parameter in parameters)
{
cmd.Parameters.Add(parameters);
}
}
con.Open();
//CommandBehavior.CloseConnection参数指示关闭reader对象时关闭与其关联的Connection对象
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
///
/// 执行一个查询,返回结果集的首行首列。忽略其他行,其他列
///
/// 要执行的SQl命令
///
public Object ExecuteScalar(string sql)
{
return ExecuteScalar(sql, CommandType.Text, null);
}
///
///
///
///
///
///
public Object ExecuteScalar(string sql, CommandType commandType)
{
return ExecuteScalar(sql, commandType, null);
}
///
///
///
///
/// 参数类型
///
///
public Object ExecuteScalar(string sql,CommandType commandType, SqlParameter[] parameters)
{
Object result=null;
SqlConnection con=new SqlConnection(connectionString);
SqlCommand cmd=new SqlCommand(sql,con);
cmd.CommandType= commandType;
if(parameters!=null)
{
foreach (SqlParameter parapmeter in parameters)
{
cmd.Parameters.Add(parapmeter);
}
}
con.Open();
result=cmd.ExecuteScalar();
con.Close();
return result;
}
///
/// 对数据库进行增删改的操作
///
/// 要执行的sql命令
///
public int ExecuteNonQuery(string sql)
{
return ExecuteNonQuery(sql, CommandType.Text, null);
}
///
/// 数据库进行增删改的操作
///
/// 对数据库进行操作的sql命令
/// 要执行查询语句的类型,如存储过程或者sql文本命令
///
public int ExecuteNonQuery(string sql, CommandType commandType)
{
return ExecuteNonQuery(sql, commandType, null);
}
///
/// 对数据库进行增删改的操作
///
/// 要执行的sql语句
/// 要执行的查询语句类型,如存储过程或者sql文本命令
/// Transact-SQL语句或者存储过程的参数数组
///
public int ExecuteNonQuery(string sql, CommandType commandType, SqlParameter[] parameters)
{
int count = 0;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = commandType;
if (parameters != null)
{
foreach(SqlParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
}
con.Open();
count = cmd.ExecuteNonQuery();
con.Close();
return count;
}
///
/// 返回当前连接的数据库中所有用户创建的数据库
///
///
public DataTable GetTables()
{
DataTable table = null;
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
table = con.GetSchema("Tables");
}
return table;
}
}
如果我们建立了一个对数据库访问的通用类以后,在随数据库进行操作的时候吗,就只需要先实例化对象,然后根据自己的需要,调用相应的方法就可以完成对数据库的所有操作。这就是数据库访问层和业务逻辑层分开的好处。
这样书写的代码,可以大大的减少我们代码的复杂度,而且,繁琐度也大大的降低了。
以上就是本文的全部内容,希望对大家的学习有所帮助。