123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.SqlClient;
- using System.Data;
- using System.Configuration;
- namespace DAL
- {
- public class SqlHelper
- {
- //获得数据库连接字符串
- public static readonly string conString = ConfigurationManager.ConnectionStrings["sqlCon"].ConnectionString;
- /// <summary>
- /// 创建数据库连接对象
- /// </summary>
- /// <returns>返回数据库连接对象</returns>
- public static SqlConnection createCon()
- {
- return new SqlConnection(conString);
- }
- /// <summary>
- /// 创建执行对象
- /// </summary>
- /// <param name="cmdText">文本内容</param>
- /// <param name="cmdType">文本类型</param>
- /// <param name="param">可变参数数组</param>
- /// <returns>返回执行对象</returns>
- public static SqlCommand createCmd(string cmdText, CommandType cmdType, params SqlParameter[] param)
- {
- //实例化执行对象
- SqlCommand cmd = new SqlCommand();
- //执行连接对象
- cmd.Connection = createCon();
- //执行文本内容
- cmd.CommandText = cmdText;
- //选择文本类型
- cmd.CommandType = cmdType;
- //判断可变参数数组里是否为null 如果不为null 则添加进可变参数数组中
- if (param != null)
- foreach (SqlParameter p in param)
- cmd.Parameters.Add(p);
- //cmd.Parameters.Clear();//执行完后清理SqlParameter中的值
- //返回执行对象
- return cmd;
- }
- /// <summary>
- /// 创建ExcuteReader()方法
- /// </summary>
- /// <param name="cmdText">文本内容</param>
- /// <param name="cmdType">文本类型</param>
- /// <param name="param">可变参数数组</param>
- /// <returns>返回读取对象</returns>
- public static SqlDataReader ExcuteReader(string cmdText, CommandType cmdType, params SqlParameter[] param)
- {
- //创建执行对象
- SqlCommand cmd = createCmd(cmdText, cmdType, param);
- try
- {
- //打开连接
- cmd.Connection.Open();
- //执行读取方法
- SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- //返回读取对象
- return dr;
- }
- catch(Exception ex)
- {
- //关闭连接
- cmd.Connection.Close();
- //返回空
- return null;
- }
- }
- /// <summary>
- /// 创建ExecuteScalar()方法
- /// </summary>
- /// <param name="cmdText">文本内容</param>
- /// <param name="cmdType">文本类型</param>
- /// <param name="param">可变参数数组</param>
- /// <returns>返回第1行第1列的值</returns>
- public static Object ExecuteScalar(string cmdText, CommandType cmdType, params SqlParameter[] param)
- {
- //创建执行对象
- SqlCommand cmd = createCmd(cmdText, cmdType, param);
- try
- {
- //打开连接
- cmd.Connection.Open();
- //执行查询方法
- Object obj = cmd.ExecuteScalar();
- //返回第1行第1列的值
- return obj;
- }
- catch(Exception EX)
- {
- //关闭连接
- cmd.Connection.Close();
- //返回空
- return null;
- }
- }
- /// <summary>
- /// 创建ExecuteNonQuery()方法
- /// </summary>
- /// <param name="cmdText">文本内容</param>
- /// <param name="cmdType">文本类型</param>
- /// <param name="param">可变参数数组</param>
- /// <returns>返回受影响行数</returns>
- public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] param)
- {
- //创建执行对象
- SqlCommand cmd = createCmd(cmdText, cmdType, param);
- try
- {
- //打开连接
- cmd.Connection.Open();
- //执行ExecuteNonQuery()方法
- int count = cmd.ExecuteNonQuery();
- //关闭连接
- cmd.Connection.Close();
- //返回受影响行数
- return count;
-
- }
- catch(Exception e)
- {
- string error = e.Message.ToString();
- //关闭连接
- cmd.Connection.Close();
- //返回 0
- return 0;
- }
- }
- /// <summary>
- /// 调用存储过程
- /// </summary>
- /// <param name="cmdText"></param>
- /// <param name="cmdType"></param>
- /// <param name="param"></param>
- /// <returns>返回DataTable类型的数据</returns>
- public static DataTable TransferProcedure(string cmdText, CommandType cmdType, params SqlParameter[] param)
- {
- //创建执行对象
- SqlCommand cmd = createCmd(cmdText, cmdType, param);
- try
- {
- //打开连接
- cmd.Connection.Open();
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- //关闭连接
- cmd.Connection.Close();
- if (dt != null)
- return dt;
- else
- return null;
- }
- catch
- {
- //关闭连接
- cmd.Connection.Close();
- //返回 0
- return null;
- }
- }
- }
- }
|