第一步:创建一个名为SqlDBHelper的类,用来作为联通数据库和系统之间的桥梁。
第二步:引入命名空间,如果System.Configuration、System.Transcations这两个命名空间找不到,可在项目目录下的【引用】=》添加引用【框架】中找到,添加引用即可。
1 using System.Data; 2 using System.Data.SqlClient; 3 using System.Configuration; 4 using System.Transactions;
第三步:在配置文件中添加数据库连接地址。
1 <configuration> 2 <startup> 3 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 4 </startup> 5 <connectionStrings> 6 <add name="MES" connectionString="Data Source=数据库IP;Database=数据库名;User ID=用户名;Password=连接密码;" providerName="System.Data.SqlClient;"/> 7 </connectionStrings> 8 </configuration>
第四部:在SqlDBHelper中编写读取配置文件中连接数据库字段:
1 private static readonly string connectionString = ConfigurationManager.ConnectionStrings["MES"].ToString();
如果ConfigurationManager提示错误,同第二步操作即可。
第五步:编写操作Sql命令的方法(增删改查):
1 public static string DataBase 2 { 3 get { 4 SqlConnection conn = new SqlConnection(); 5 string db = conn.Database; //获取当前数据库或使用的数据库名称 6 conn.Dispose(); 7 return db; 8 } 9 } 10 11 #region 为SQL命令做提前的参数设定 12 /// <summary> 13 /// 为cmd做执行前的参数设定 14 /// </summary> 15 /// <param name="cmd">cmd命令对象</param> 16 /// <param name="conn">连接对象</param> 17 /// <param name="trans">事务对象</param> 18 /// <param name="cmdType">命令类型</param> 19 /// <param name="cmdText">SQL命令文本</param> 20 /// <param name="cmdParms">在命令文本中要使用的SQL参数</param> 21 private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) 22 { 23 if (conn.State != System.Data.ConnectionState.Open) //如果连接通道未打开,则打开连接通道 24 conn.Open(); 25 cmd.CommandTimeout = 60; //等待时间 26 cmd.Connection = conn; //为命令对象设置连接通道 27 cmd.CommandText = cmdText; //为命令对象设置SQL文本 28 if (trans != null) //如果存在事务,则为命令对象设置事务 29 cmd.Transaction = trans; 30 cmd.CommandType = cmdType; //设置命令类型(SQL文本/存储过程) 31 if (cmdParms != null) //如果参数集合不为空,为命令对象添加参数 32 cmd.Parameters.AddRange(cmdParms); 33 34 } 35 #endregion 36 37 38 #region sql 增删改 39 /// <summary> 40 /// 执行SQL命令 增删改(无参数) 41 /// </summary> 42 /// <param name="cmdText">SQL命令语句</param> 43 /// <returns></returns> 44 public static int ExecuteCommand(string cmdText) //重载 45 { 46 return ExecuteCommand(cmdText,null); 47 } 48 49 /// <summary> 50 /// 执行SQL命令 增删改(带参数) 51 /// </summary> 52 /// <param name="cmdText">SQL命令语句</param> 53 /// <param name="parameters">参数</param> 54 /// <returns></returns> 55 public static int ExecuteCommand(string cmdText, SqlParameter[] parameters) 56 { 57 return ExecuteCommand(cmdText,CommandType.Text,parameters); 58 } 59 60 /// <summary> 61 /// 执行SQL命令 增删改(带参数) 62 /// </summary> 63 /// <param name="cmdText">SQL命令</param> 64 /// <param name="cmdType">命令类型</param> 65 /// <param name="commandParameters">参数集合</param> 66 /// <returns></returns> 67 public static int ExecuteCommand(string cmdText, CommandType cmdType, params SqlParameter[] commandParameters) 68 { 69 using (TransactionScope transcation = new TransactionScope()) //TransactionScope 使代码块成为事务性代码 70 { 71 int res = 0; 72 try 73 { 74 SqlCommand cmd = new SqlCommand(); 75 using (SqlConnection conn = new SqlConnection(connectionString)) 76 { 77 PrepareCommand(cmd,conn,null,cmdType,cmdText,commandParameters); 78 79 res = cmd.ExecuteNonQuery(); //执行SQL并返回受影响的行数 80 transcation.Complete(); //指示范围内的操作都已经完成 81 } 82 } 83 catch (Exception ex) 84 { 85 throw ex; 86 } 87 return res; 88 } 89 } 90 91 /// <summary> 92 /// 执行SQL命令 增删改 93 /// </summary> 94 /// <param name="cmdText">SQL命令</param> 95 /// <param name="trans">事务对象</param> 96 /// <param name="cmdType">命令类型</param> 97 /// <param name="parameters">命令参数</param> 98 /// <returns></returns> 99 public static int ExecuteCommand(string cmdText, SqlTransaction trans, CommandType cmdType, SqlParameter[] parameters) 100 { 101 SqlCommand cmd = new SqlCommand(); 102 PrepareCommand(cmd,trans.Connection,trans,cmdType,cmdText,parameters); 103 int res = 0; 104 try 105 { 106 res = cmd.ExecuteNonQuery(); 107 } 108 catch (Exception ex) 109 { 110 throw ex; 111 } 112 cmd.Parameters.Clear(); 113 return res; 114 } 115 #endregion 116 117 #region sql 查询操作 118 /// <summary> 119 /// 执行SQL命令 查询 120 /// </summary> 121 /// <param name="sqlStr">SQL命令语句</param> 122 /// <returns></returns> 123 public static DataTable GetDataTable(string sqlStr) 124 { 125 return GetDataTable(sqlStr,null); 126 } 127 128 /// <summary> 129 /// 执行SQL命令 查询 130 /// </summary> 131 /// <param name="sqlStr">SQL命令</param> 132 /// <param name="parameters">参数集合</param> 133 /// <returns></returns> 134 public static DataTable GetDataTable(string sqlStr, SqlParameter[] parameters) 135 { 136 return GetDataTable(sqlStr,CommandType.Text,parameters); 137 } 138 139 /// <summary> 140 /// 执行SQL命令 141 /// </summary> 142 /// <param name="sqlStr">SQL命令</param> 143 /// <param name="cmdType">命令类型</param> 144 /// <param name="parameters">参数集合</param> 145 /// <returns></returns> 146 public static DataTable GetDataTable(string sqlStr, CommandType cmdType, SqlParameter[] parameters) 147 { 148 using (SqlConnection connection = new SqlConnection(connectionString)) 149 { 150 SqlCommand cmd = new SqlCommand(sqlStr,connection); 151 cmd.CommandTimeout = 60; 152 cmd.CommandType = cmdType; 153 if (parameters != null) 154 cmd.Parameters.AddRange(parameters); 155 SqlDataAdapter da = new SqlDataAdapter(cmd); 156 DataTable dt = new DataTable(); 157 try 158 { 159 da.Fill(dt); 160 } 161 catch (Exception ex) 162 { 163 throw ex; 164 } 165 cmd.Parameters.Clear(); 166 return dt; 167 } 168 } 169 170 171 public static DataTable GetDataTable(string sqlStr,string con,int i) 172 { 173 SqlConnection conn = new SqlConnection(con); 174 conn.Open(); 175 SqlCommand cmd = new SqlCommand(sqlStr,conn); 176 SqlDataAdapter da = new SqlDataAdapter(cmd); 177 DataTable dt = new DataTable(); 178 da.Fill(dt); 179 conn.Close(); 180 cmd.Dispose(); 181 da.Dispose(); 182 return dt; 183 } 184 185 /// <summary> 186 /// 执行SQL 查询 187 /// </summary> 188 /// <param name="cmdText">SQL命令语句</param> 189 /// <returns></returns> 190 public static object GetScalar(string cmdText) 191 { 192 return GetScalar(cmdText,null); 193 } 194 195 /// <summary> 196 /// 执行SQL 查询 197 /// </summary> 198 /// <param name="cmdText">SQL命令</param> 199 /// <param name="parameters">参数集合</param> 200 /// <returns>第一行第一列(object类型)</returns> 201 public static object GetScalar(string cmdText, SqlParameter[] parameters) 202 { 203 return GetScalar(cmdText,CommandType.Text,parameters); 204 } 205 206 /// <summary> 207 /// 执行SQL 查询 208 /// </summary> 209 /// <param name="cmdText">SQL命令</param> 210 /// <param name="cmdType">命令类型</param> 211 /// <param name="parameters">参数集合</param> 212 /// <returns>第一行第一列的值(object类型)</returns> 213 public static object GetScalar(string cmdText, CommandType cmdType, SqlParameter[] parameters) 214 { 215 object res = 0; 216 SqlCommand cmd = new SqlCommand(); 217 using (SqlConnection conn = new SqlConnection(connectionString)) 218 { 219 PrepareCommand(cmd,conn,null,cmdType,cmdText,parameters); 220 try 221 { 222 res = cmd.ExecuteScalar(); 223 } 224 catch(Exception ex) 225 { 226 throw ex; 227 } 228 cmd.Parameters.Clear(); 229 return res; 230 } 231 } 232 233 public static void Insert_NewData(DataTable dt, string tableName) 234 { 235 SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(connectionString,SqlBulkCopyOptions.UseInternalTransaction); 236 sqlBulkCopy.DestinationTableName = tableName;//数据库中的表名 237 238 sqlBulkCopy.WriteToServer(dt); 239 sqlBulkCopy.Close(); 240 } 241 #endregion
至此,SqlDBHelper类完成,在需要时直接调用即可。