为空变量转换方法
View Code
1 public static object FromDbValue(object value) 2 { 3 if (value == DBNull.Value) 4 { 5 return null; 6 } 7 else 8 { 9 return value; 10 } 11 } 12 13 public static object ToDbValue(object value) 14 { 15 if (value == null) 16 { 17 return DBNull.Value; 18 } 19 else 20 { 21 return value; 22 } 23 }
CS程序全程序范围内保存值:Application.Current.Properties["OperatorId"];
SqlHelper写法:
View Code
View Code
1 public class SQLHelper 2 { 3 private SqlConnection conn = null; 4 private SqlCommand cmd = null; 5 private SqlDataReader sdr = null; 6 public SQLHelper() 7 { 8 string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; 9 conn = new SqlConnection(connStr); 10 } 11 private SqlConnection GetConn() 12 { 13 if (conn.State == ConnectionState.Closed) 14 { 15 conn.Open(); 16 } 17 return conn; 18 } 19 /// <summary> 20 /// 执行不带参数的增删改SQL语句或存储过程 21 /// </summary> 22 /// <param name="cmdText">增删改SQL语句或存储过程</param> 23 /// <param name="ct">命令类型</param> 24 /// <returns></returns> 25 public int ExecuteNonQuery(string cmdText, CommandType ct) 26 { 27 int res; 28 try 29 { 30 cmd = new SqlCommand(cmdText, GetConn()); 31 cmd.CommandType = ct; 32 res = cmd.ExecuteNonQuery(); 33 } 34 catch (Exception ex) 35 { 36 37 throw ex; 38 } 39 finally 40 { 41 if (conn.State == ConnectionState.Open) 42 { 43 conn.Close(); 44 } 45 } 46 return res; 47 } 48 49 /// <summary> 50 /// 执行带参数的增删改SQL语句或存储过程 51 /// </summary> 52 /// <param name="cmdText">增删改SQL语句或存储过程</param> 53 /// <param name="ct">命令类型</param> 54 /// <returns></returns> 55 public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct) 56 { 57 int res; 58 using (cmd = new SqlCommand(cmdText, GetConn())) 59 { 60 cmd.CommandType = ct; 61 cmd.Parameters.AddRange(paras); 62 res = cmd.ExecuteNonQuery(); 63 } 64 return res; 65 } 66 67 /// <summary> 68 /// 执行查询SQL语句或存储过程 69 /// </summary> 70 /// <param name="cmdText">查询SQL语句或存储过程</param> 71 /// <param name="ct">命令类型</param> 72 /// <returns></returns> 73 public DataTable ExecuteQuery(string cmdText, CommandType ct) 74 { 75 DataTable dt = new DataTable(); 76 cmd = new SqlCommand(cmdText,GetConn()); 77 cmd.CommandType = ct; 78 using(sdr=cmd.ExecuteReader(CommandBehavior.CloseConnection)) 79 { 80 dt.Load(sdr); 81 } 82 return dt; 83 } 84 85 /// <summary> 86 /// 执行带参数的查询SQL语句或存储过程 87 /// </summary> 88 /// <param name="cmdText">查询SQL语句或存储过程</param> 89 /// <param name="paras">参数集合</param> 90 /// <param name="ct">命令类型</param> 91 /// <returns></returns> 92 public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct) 93 { 94 DataTable dt = new DataTable(); 95 cmd = new SqlCommand(cmdText, GetConn()); 96 cmd.CommandType = ct; 97 cmd.Parameters.AddRange(paras); 98 using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) 99 { 100 dt.Load(sdr); 101 } 102 return dt; 103 } 104 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Configuration; 6 using System.Data.SqlClient; 7 using System.Data; 8 9 namespace HRMSys.DAL 10 { 11 static class SqlHelper 12 { 13 //app.config文件的继承: 14 15 public static readonly string connstr = 16 ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; 17 18 public static int ExecuteNonQuery(string sql, 19 params SqlParameter[] parameters) 20 { 21 using (SqlConnection conn = new SqlConnection(connstr)) 22 { 23 conn.Open(); 24 using (SqlCommand cmd = conn.CreateCommand()) 25 { 26 cmd.CommandText = sql; 27 cmd.Parameters.AddRange(parameters); 28 return cmd.ExecuteNonQuery(); 29 } 30 } 31 } 32 33 public static object ExecuteScalar(string sql, 34 params SqlParameter[] parameters) 35 { 36 using (SqlConnection conn = new SqlConnection(connstr)) 37 { 38 conn.Open(); 39 using (SqlCommand cmd = conn.CreateCommand()) 40 { 41 cmd.CommandText = sql; 42 cmd.Parameters.AddRange(parameters); 43 return cmd.ExecuteScalar(); 44 } 45 } 46 } 47 48 public static DataTable ExecuteDataTable(string sql, 49 params SqlParameter[] parameters) 50 { 51 using (SqlConnection conn = new SqlConnection(connstr)) 52 { 53 conn.Open(); 54 using (SqlCommand cmd = conn.CreateCommand()) 55 { 56 cmd.CommandText = sql; 57 cmd.Parameters.AddRange(parameters); 58 59 DataSet dataset = new DataSet(); 60 SqlDataAdapter adapter = new SqlDataAdapter(cmd); 61 adapter.Fill(dataset); 62 return dataset.Tables[0]; 63 } 64 } 65 } 66 } 67 }