|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace SQLDB
{
public class SqlHelp
{ //定义链接数据库字符串
private static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();
//定义conn
private static SqlConnection conn;
//定义cmd
private static SqlCommand cmd;
//实例化DataSet
private static DataSet ds = new DataSet();
#region 链接模型下标准化的数据通用访问类的三个方法
/// <summary>
/// 返回第一行第一列数据
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns>返回第一行第一列数据</returns>
public static object GetExecuteScalar(string sql)
{
//1.实例化conn
conn = new SqlConnection(connString);
//2.实例化cmd
cmd = new SqlCommand(sql, conn);
//3执行
try
{
//4.打开
conn.Open();
//5.返回
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
//6.关闭
conn.Close();
}
}
/// <summary>
/// 读取所有数据到DataReader
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns>返回所有数据到DataReader</returns>
public static SqlDataReader GetExecuteReader(string sql)
{
//实例化conn
conn = new SqlConnection(connString);
//实例化cmd
cmd = new SqlCommand(sql, conn);
//执行
try
{
conn.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 执行增删改
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns>返回增删改受影响的行数整数类型</returns>
public static int GetExecuteNonQuery(string sql)
{
//实例化conn
conn = new SqlConnection(connString);
//实例化cmd
cmd = new SqlCommand(sql, conn);
//执行
try
{
conn.Open();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
#endregion
#region 非链接模型下的数据通用访问类的两种方法
/// <summary>
/// 获取DataSet的Table中没有名称
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns>返回没有表名称的DataSet</returns>
public static DataSet GetDataSetNotTableName(string sql)
{
//实例化conn
conn = new SqlConnection(connString);
//实例化CMD
cmd = new SqlCommand(sql, conn);
//执行
try
{
conn.Open();
//实例化dataReader
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//填充到DataSet
sda.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
return ds;
}
/// <summary>
/// 获得所有信息到DataSet内存数据库
/// </summary>
/// <param name="dic">键值对《键{表名称},值{查询语句}>》</param>
/// <param name="KeyName">主键的列</param>
/// <returns>返回带有主键和Table表名称的内存数据库</returns>
public static DataSet GetDataSet(Dictionary<string, string> dic, string KeyName)
{
//实例化conn
conn = new SqlConnection(connString);
//执行
try
{
foreach (KeyValuePair<string, string> item in dic)
{
cmd = new SqlCommand(item.Value, conn);
//实例化DataAdapter
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//定义sda的类型
sda.SelectCommand = cmd;
//实例化commandB 记录DataSet的操作过程,包括增删改
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
//打开
conn.Open();
//填充DataSet
sda.Fill(ds, item.Key);
//定义表的主键
ds.Tables[item.Key].PrimaryKey = new DataColumn[] { ds.Tables[item.Key].Columns[KeyName] };
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
//返回
return ds;
}
#endregion
#region 链接模型下标准化的带有参数数组的数据通用访问类(重载)
/// <summary>
/// 参数化SQL语句的获得获得第一行第一列的数据
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">SQL参数数组</param>
/// <returns>返回object类型</returns>
public static object GetExecuteScalar(string sql, SqlParameter[] para)
{
//实例化conn
conn = new SqlConnection(connString);
//实例化cmd
cmd = new SqlCommand(sql, conn);
//执行
try
{
//打开
conn.Open();
if (para != null)//如果参数数组不为空的话
{
//执行参数数组
cmd.Parameters.AddRange(para);
}
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 参数化SQL语句的获取所有数据的链接模型
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">参数数组</param>
/// <returns>返回DataReader</returns>
public static SqlDataReader GetExecuteReader(string sql, SqlParameter[] para)
{
//实例化conn
conn = new SqlConnection(connString);
//实例化cmd
cmd = new SqlCommand(sql, conn);
//执行
try
{
conn.Open();
if (para != null)//如果参数数组不为空的话
{
//执行参数数组
cmd.Parameters.AddRange(para);
}
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 参数化SQL语句的执行增删改
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">参数数组</param>
/// <returns>返回受影响的行数</returns>
public static int GetExecuteNonQuery(string sql, SqlParameter[] para)
{
//实例化conn
conn = new SqlConnection(connString);
//实例化cmd
cmd = new SqlCommand(sql, conn);
//执行
try
{
//打开
conn.Open();
if (para != null)//如果参数数组不为空的话
{
//执行参数数组
cmd.Parameters.AddRange(para);
}
//返回所有数据
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
#endregion
|
|