C#SQL数据库的访问类
///<summary>
/// SQL数据库的访问类
///</summary>
public class SQLHelper
{
///<summary>
///执⾏⾮查询
///</summary>
///<param name="connectionString">连接字符串</param>
///<param name="cmdType">命令类型</param>
/
//<param name="cmdText">命令的⽂本</param>
///<param name="commandParameters">命令的参数</param>
///<returns>返回影响的结果集⾏数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
///<summary>
///执⾏⾮查询
///</summary>
///<param name="connection">数据库连接</param>
///<param name="cmdType">命令的类型</param>
///<param name="cmdText">命令的⽂本</param>
///<param name="commandParameters">命令的参数</param>
///<returns>返回影响的结果集⾏数</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
///<summary>
///执⾏⾮查询
///</summary>
///<param name="trans">事务</param>
///<param name="cmdType">命令的类型</param>
///<param name="cmdText">命令的⽂本</param>
///<param name="commandParameters">命令的参数</param>
///<returns>返回影响的结果集⾏数</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
///<summary>
///执⾏查询
///</summary>
///<param name="connectionString">练级字符串</param>
///<param name="cmdType">命令的类型</param>
///<param name="cmdText">命令的⽂本</param>
///<param name="commandParameters">命令的参数数组</param>
///<returns>返回结果集</returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
///<summary>
///执⾏查询
///</summary>
///<param name="connectionString">练级字符串</param>
///<param name="cmdType">命令的类型</param>
///<param name="cmdText">命令的⽂本</param>
///<param name="commandParameters">命令的参数数组</param>
/
//<returns>返回结果集</returns>
public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
sqltransaction什么意思{
PrepareCommand(cmd, transaction.Connection, transaction, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.Default);
cmd.Parameters.Clear();
return rdr;
}
catch
{
throw;
}
}
///<summary>
///读取数据
///</summary>
///<param name="connectionString">数据库连接字符串</param>
///<param name="cmdType">数据命令类型</param>
/
//<param name="cmdText">数据命令的⽂本</param>
///<param name="cmd">需要被执⾏的数据命令</param>
///<param name="commandParameters">数据命令的参数</param>
///<returns>返回影响的结果集⾏数</returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, out SqlCommand cmd, params SqlParameter[] commandParameters) {
cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return rdr;
}
catch
{
conn.Close();
throw;
}
}
//匹配参数
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论