C#编写的一个SQL工具类
1.引用文件
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
2.SQL操作类
public class Sqlhelper
{
public static readonly string connString = "Data Source=XXX;Initial Catalog=XXX;Persist Security Info=True;User ID=XXX;Password=XXX";
//public static readonly string connectionString = connString;
#region ExecuteNonQuery(如果是增,删,修)
/// <summary>
/// 执行sql命令
/// </summary>
///
///
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">sql语句/参数化sql语句/存储过程名</param>
/// <param name="commandParameters">参数</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, commandType, conn, commandText, commandParameters);
int val = cmd.ExecuteNonQuery();
return val;
}
}
/// <summary>
/// 执行Sql Server存储过程
/// 注意:不能执行有out 参数的存储过程
/// <param name="connectionString">连接字符串</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">对象参数</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, spName, parameterValues);
int val = cmd.ExecuteNonQuery();
return val;
}
}
//单纯执行SQL
public static int ExecuteNonQuery(string connectionString, string cmdsql)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(cmdsql);
//打开连接
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
int val = cmd.ExecuteNonQuery();
conn.Close();
return val;
}
}
public static int ExecuteNonQuery(string cmdsql)
{
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand(cmdsql);
//打开连接
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
int val = cmd.ExecuteNonQuery();
conn.Close();
return val;
}
}
#endregion
public static int ExecNonQueryProc(string spName, params object[] param)
{
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand();
PrepareCmd(cmd, conn, spName, param);
int val = cmd.ExecuteNonQuery();
return val;
}
}
#region ExecuteReader(如果只是读数据)
/// <summary>
/// 执行sql命令
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">sql语句/参数化sql语句/存储过程名</param>
/// <param name="commandParameters">参数</param>
/// <returns>SqlDataReader 对象</returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
SqlConnection conn = new SqlConnection(connectionString);
try
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, commandType, conn, commandText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return rdr;
}
catch
{
conn.Close();
throw;
}
}
/// <summary>
/// 执行Sql Server存储过程
/// 注意:不能执行有out 参数的存储过程
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">对象参数</param>
/// <returns>受影响的行数</returns>
public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
{
SqlConnection conn = new SqlConnection(connectionString);
try
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, spName, parameterValues);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return rdr;
}
catch
{
conn.Close();
throw;
}
}
#endregion
#region ExecuteDataset(如果查询一个数据集)
/// <summary>
/// 执行Sql Server存储过程
/// 注意:不能执行有out 参数的存储过程
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">对象参数</param>
/// <returns>DataSet对象</returns>
public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, spName, parameterValues);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
public static DataTable ExecuteDataTable(string spName)
{
SqlConnection conn = new SqlConnection(connString);
//打开连接
if (conn.State != ConnectionState.Open)
conn.Open();
SqlCommand cmd = new SqlCommand(spName, conn);
cmd.CommandTimeout = 12000;
using (SqlDataAdapter sda1 = new SqlDataAdapter(cmd))
{
DataTable dt1 = new DataTable();
sda1.Fill(dt1);
return dt1;
}
}
/// <summary>
/// 执行Sql 命令
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">sql语句/参数化sql语句/存储过程名</param>
/// <param name="commandParameters">参数</param>
/// <returns>DataSet 对象</returns>
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, commandType, conn, commandText, commandParameters);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
#endregion
#region ExecuteScalar (如果你只查询一个值)
/// <summary>
/// 执行Sql 命令
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">sql语句/参数化sql语句/存储过程名</param>
/// <param name="commandParameters">参数</param>
/// <returns>执行结果对象</returns>
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, commandType, conn, commandText, commandParameters);
object val = cmd.ExecuteScalar();
return val;
}
}
/// <summary>
/// 执行Sql Server存储过程
/// 注意:不能执行有out 参数的存储过程
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">对象参数</param>
/// <returns>执行结果对象</returns>
public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, conn, spName, parameterValues);
object val = cmd.ExecuteScalar();
return val;
}
}
#endregion
#region Private Method
/// <summary>
/// 设置一个等待执行的SqlCommand对象
/// </summary>
/// <param name="cmd">SqlCommand 对象,不允许空对象</param>
/// <param name="conn">SqlConnection 对象,不允许空对象</param>
/// <param name="commandText">Sql 语句</param>
/// <param name="cmdParms">SqlParameters 对象,允许为空对象</param>
private static void PrepareCommand(SqlCommand cmd, CommandType commandType, SqlConnection conn, string commandText, SqlParameter[] cmdParms)
{
//打开连接
if (conn.State != ConnectionState.Open)
conn.Open();
//设置SqlCommand对象
cmd.Connection = conn;
cmd.CommandText = commandText;
cmd.CommandType = commandType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
/// <summary>
/// 设置一个等待执行存储过程的SqlCommand对象
/// </summary>
/// <param name="cmd">SqlCommand 对象,不允许空对象</param>
/// <param name="conn">SqlConnection 对象,不允许空对象</param>
/// <param name="spName">Sql 语句</param>
/// <param name="parameterValues">不定个数的存储过程参数,允许为空</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, string spName, params object[] parameterValues)
{
//打开连接
if (conn.State != ConnectionState.Open)
conn.Open();
//设置SqlCommand对象
cmd.Connection = conn;
cmd.CommandText = spName;
cmd.CommandType = CommandType.StoredProcedure;
//获取存储过程的参数
SqlCommandBuilder.DeriveParameters(cmd);
//移除Return_Value 参数
cmd.Parameters.RemoveAt(0);
//设置参数值
if (parameterValues != null)
{
for (int i = 0; i < cmd.Parameters.Count; i++)
{
cmd.Parameters[i].Value = parameterValues[i];
}
}
}
private static void PrepareCmd(SqlCommand cmd, SqlConnection conn, string spName, params object[] cmdParms)
{
//打开连接
if (conn.State != ConnectionState.Open)
conn.Open();
//设置SqlCommand对象
cmd.Connection = conn;
cmd.CommandText = spName;
cmd.CommandType = CommandType.StoredProcedure;
获取存储过程的参数
//SqlCommandBuilder.DeriveParameters(cmd);
移除Return_Value 参数
//cmd.Parameters.RemoveAt(0);
//设置参数值
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
#endregion
}
后续只需要在项目里引入这个工具类,就可以简化对数据库的操作了。