123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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 SqlHelperOA2023
- {
- //获得数据库连接字符串
- public static readonly string conString = ConfigurationManager.ConnectionStrings["sqlConNew"].ConnectionString;
- /// <summary>
- /// 创建数据库连接对象
- /// </summary>
- /// <returns>返回数据库连接对象</returns>
- public static SqlConnection createCon()
- {
- return new SqlConnection(conString);
- }
- public static DataTable QueryAll(string sql)
- {
- SqlConnection conn = new SqlConnection(conString);
- try
- {
- conn.Open();
- SqlCommand com = new SqlCommand(sql, conn);
- SqlDataAdapter da = new SqlDataAdapter(com);
- DataSet ds = new DataSet();
- da.Fill(ds);
- return ds.Tables[0];
- }
- catch (Exception)
- {
- return null;
- }
- finally
- {
- conn.Close();
- }
- }
- /// <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>
- /// 创建ExecuteNonQuery()方法Edit
- /// </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>
- /// 创建ExecuteScalar()方法Add
- /// </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;
- }
- }
- }
- }
|