第一步:建立一個名爲SqlDBHelper的類,用來做爲聯通數據庫和系統之間的橋樑。sql
第二步:引入命名空間,若是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中編寫讀取配置文件中鏈接數據庫字段:ide
1 private static readonly string connectionString = ConfigurationManager.ConnectionStrings["MES"].ToString();
若是ConfigurationManager提示錯誤,同第二步操做便可。spa
第五步:編寫操做Sql命令的方法(增刪改查):code
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類完成,在須要時直接調用便可。對象