话不多说,直接上代码!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace RoomManagementSystem.DAL
{
public class DBHelper
{
/// <summary>
/// 连接字符串
/// </summary>
public static string ConnStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();
#region 执行工具
/// <summary>
/// 执行工具
/// </summary>
/// <param name="con">连接对象</param>
/// <param name="cmd">工具</param>
/// <param name="paras">参数数组</param>
/// <param name="sql">SQL语句</param>
/// <param name="cmdType">命令类型</param>
public static void PrepareCommand(SqlConnection con, SqlCommand cmd, SqlParameter[] paras, string sql, CommandType cmdType)
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
cmd.Connection = con;
cmd.CommandType = cmdType;
cmd.CommandText = sql;
if (paras == null)
{
return;
}
else
{
foreach (SqlParameter p in paras)
{
cmd.Parameters.Add(p);
}
}
}
#endregion
#region 执行添加、删除、修改的方法
/// <summary>
/// 执行添加、删除、修改的方法
/// </summary>
/// <param name="sql"></param>
/// <param name="cmdType"></param>
/// <param name="paras"></param>
/// <returns>受影响行数</returns>
public static int ExecuteNonQuery(string sql, CommandType cmdType, params SqlParameter[] paras)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
PrepareCommand(conn, cmd, paras, sql, cmdType);
int result = cmd.ExecuteNonQuery();
return result;
}
}
#endregion
#region 查询首行首列
/// <summary>
/// 查询首行首列
/// </summary>
/// <param name="sql"></param>
/// <param name="cmdType"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] paras)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
PrepareCommand(conn, cmd, paras, sql, cmdType);
return cmd.ExecuteScalar();
}
}
#endregion
#region 执行SQL语句,返回SqlDataReader对象
/// <summary>
/// 执行SQL语句,返回SqlDataReader对象
/// </summary>
/// <param name="sql"></param>
/// <param name="cmdType">命令类型</param>
/// <param name="paras"></param>
/// <returns></returns>
public static SqlDataReader GetDataReader(string sql, CommandType cmdType, params SqlParameter[] paras)
{
SqlCommand cmd = new SqlCommand();
try
{
using (SqlConnection conn = new SqlConnection(ConnStr))
{
PrepareCommand(conn, cmd, paras, sql, cmdType);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader;
}
}
catch (Exception e)
{
throw e;
}
}
#endregion
#region 获取datatable
/// <summary>
/// 获取datatable
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="cmdType">命令类型</param>
/// <param name="paras">参数</param>
/// <returns></returns>
public static DataTable GetDataTable(string sql, CommandType cmdType, params SqlParameter[] paras)
{
SqlCommand cmd = new SqlCommand();
DataTable dt = null;
try
{
using (SqlConnection con = new SqlConnection(ConnStr))
{
PrepareCommand(con, cmd, paras, sql, cmdType);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
}
return dt;
}
catch (Exception e)
{
throw e;
}
}
#endregion
}
}
如有问题,请多指教,谢谢!!!