1.新建一个解决方案
新建项目-其他项目类型-解决方案
2.新建一个空项目在里面新建三层 注意类库右键解决方案添加
DAL
BLL
Model
3.DAL中新建用于查询数据库的类 DBHelper
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Configuration; 5 using System.Data; 6 using System.Data.SqlClient; 7 8 namespace DAL 9 { 10 public class DBHelper 11 { 12 13 //连接字符串 这里把数据库连接字符串放到web.config中 14 //注意:当前上下文中不存在名称“ConfigurationManager” 是DAL没有添加using System.Configuration;引用 15 static string strConn = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString(); 16 17 18 19 #region 执行查询,返回DataTable对象----------------------- 20 21 22 23 public static DataTable GetTable(string strSQL) 24 { 25 return GetTable(strSQL, null); 26 } 27 public static DataTable GetTable(string strSQL, SqlParameter[] pas) 28 { 29 return GetTable(strSQL, pas, CommandType.Text); 30 } 31 /// <summary> 32 /// 执行查询,返回DataTable对象 33 /// </summary> 34 /// <param name="strSQL">sql语句</param> 35 /// <param name="pas">参数数组</param> 36 /// <param name="cmdtype">Command类型</param> 37 /// <returns>DataTable对象</returns> 38 public static DataTable GetTable(string strSQL, SqlParameter[] pas, CommandType cmdtype) 39 { 40 DataTable dt = new DataTable(); ; 41 using (SqlConnection conn = new SqlConnection(strConn)) 42 { 43 SqlDataAdapter da = new SqlDataAdapter(strSQL, conn); 44 da.SelectCommand.CommandType = cmdtype; 45 if (pas != null) 46 { 47 da.SelectCommand.Parameters.AddRange(pas); 48 } 49 da.Fill(dt); 50 } 51 return dt; 52 } 53 54 55 56 #endregion 57 58 59 60 61 #region 执行查询,返回DataSet对象------------------------- 62 63 64 65 66 public static DataSet GetDataSet(string strSQL) 67 { 68 return GetDataSet(strSQL, null); 69 } 70 71 public static DataSet GetDataSet(string strSQL, SqlParameter[] pas) 72 { 73 return GetDataSet(strSQL, pas, CommandType.Text); 74 } 75 /// <summary> 76 /// 执行查询,返回DataSet对象 77 /// </summary> 78 /// <param name="strSQL">sql语句</param> 79 /// <param name="pas">参数数组</param> 80 /// <param name="cmdtype">Command类型</param> 81 /// <returns>DataSet对象</returns> 82 public static DataSet GetDataSet(string strSQL, SqlParameter[] pas, CommandType cmdtype) 83 { 84 DataSet dt = new DataSet(); ; 85 using (SqlConnection conn = new SqlConnection(strConn)) 86 { 87 SqlDataAdapter da = new SqlDataAdapter(strSQL, conn); 88 da.SelectCommand.CommandType = cmdtype; 89 if (pas != null) 90 { 91 da.SelectCommand.Parameters.AddRange(pas); 92 } 93 da.Fill(dt); 94 } 95 return dt; 96 } 97 #endregion 98 99 100 101 102 103 #region 执行非查询存储过程和SQL语句----------------------------- 104 105 106 107 108 public static int ExcuteProc(string ProcName) 109 { 110 return ExcuteSQL(ProcName, null, CommandType.StoredProcedure); 111 } 112 113 public static int ExcuteProc(string ProcName, SqlParameter[] pars) 114 { 115 return ExcuteSQL(ProcName, pars, CommandType.StoredProcedure); 116 } 117 118 public static int ExcuteSQL(string strSQL) 119 { 120 return ExcuteSQL(strSQL, null); 121 } 122 123 public static int ExcuteSQL(string strSQL, SqlParameter[] paras) 124 { 125 return ExcuteSQL(strSQL, paras, CommandType.Text); 126 } 127 128 /// 执行非查询存储过程和SQL语句 129 /// 增、删、改 130 /// </summary> 131 /// <param name="strSQL">要执行的SQL语句</param> 132 /// <param name="paras">参数列表,没有参数填入null</param> 133 /// <param name="cmdType">Command类型</param> 134 /// <returns>返回影响行数</returns> 135 public static int ExcuteSQL(string strSQL, SqlParameter[] paras, CommandType cmdType) 136 { 137 int i = 0; 138 using (SqlConnection conn = new SqlConnection(strConn)) 139 { 140 SqlCommand cmd = new SqlCommand(strSQL, conn); 141 cmd.CommandType = cmdType; 142 if (paras != null) 143 { 144 cmd.Parameters.AddRange(paras); 145 } 146 conn.Open(); 147 i = cmd.ExecuteNonQuery(); 148 conn.Close(); 149 } 150 return i; 151 152 } 153 154 155 #endregion 156 157 158 159 160 161 162 163 164 #region 执行查询返回第一行,第一列--------------------------------- 165 166 167 168 169 public static int ExcuteScalarSQL(string strSQL) 170 { 171 return ExcuteScalarSQL(strSQL, null); 172 } 173 174 public static int ExcuteScalarSQL(string strSQL, SqlParameter[] paras) 175 { 176 return ExcuteScalarSQL(strSQL, paras, CommandType.Text); 177 } 178 public static int ExcuteScalarProc(string strSQL, SqlParameter[] paras) 179 { 180 return ExcuteScalarSQL(strSQL, paras, CommandType.StoredProcedure); 181 } 182 /// <summary> 183 /// 执行SQL语句,返回第一行,第一列 184 /// </summary> 185 /// <param name="strSQL">要执行的SQL语句</param> 186 /// <param name="paras">参数列表,没有参数填入null</param> 187 /// <returns>返回影响行数</returns> 188 public static int ExcuteScalarSQL(string strSQL, SqlParameter[] paras, CommandType cmdType) 189 { 190 int i = 0; 191 using (SqlConnection conn = new SqlConnection(strConn)) 192 { 193 SqlCommand cmd = new SqlCommand(strSQL, conn); 194 cmd.CommandType = cmdType; 195 if (paras != null) 196 { 197 cmd.Parameters.AddRange(paras); 198 } 199 conn.Open(); 200 i = Convert.ToInt32(cmd.ExecuteScalar()); 201 conn.Close(); 202 } 203 return i; 204 205 } 206 207 208 #endregion 209 210 211 212 213 214 215 216 217 218 #region 查询获取单个值------------------------------------ 219 220 221 222 223 /// <summary> 224 /// 调用不带参数的存储过程获取单个值 225 /// </summary> 226 /// <param name="ProcName"></param> 227 /// <returns></returns> 228 public static object GetObjectByProc(string ProcName) 229 { 230 return GetObjectByProc(ProcName, null); 231 } 232 /// <summary> 233 /// 调用带参数的存储过程获取单个值 234 /// </summary> 235 /// <param name="ProcName"></param> 236 /// <param name="paras"></param> 237 /// <returns></returns> 238 public static object GetObjectByProc(string ProcName, SqlParameter[] paras) 239 { 240 return GetObject(ProcName, paras, CommandType.StoredProcedure); 241 } 242 /// <summary> 243 /// 根据sql语句获取单个值 244 /// </summary> 245 /// <param name="strSQL"></param> 246 /// <returns></returns> 247 public static object GetObject(string strSQL) 248 { 249 return GetObject(strSQL, null); 250 } 251 /// <summary> 252 /// 根据sql语句 和 参数数组获取单个值 253 /// </summary> 254 /// <param name="strSQL"></param> 255 /// <param name="paras"></param> 256 /// <returns></returns> 257 public static object GetObject(string strSQL, SqlParameter[] paras) 258 { 259 return GetObject(strSQL, paras, CommandType.Text); 260 } 261 262 /// <summary> 263 /// 执行SQL语句,返回首行首列 264 /// </summary> 265 /// <param name="strSQL">要执行的SQL语句</param> 266 /// <param name="paras">参数列表,没有参数填入null</param> 267 /// <returns>返回的首行首列</returns> 268 public static object GetObject(string strSQL, SqlParameter[] paras, CommandType cmdtype) 269 { 270 object o = null; 271 using (SqlConnection conn = new SqlConnection(strConn)) 272 { 273 SqlCommand cmd = new SqlCommand(strSQL, conn); 274 cmd.CommandType = cmdtype; 275 if (paras != null) 276 { 277 cmd.Parameters.AddRange(paras); 278 279 } 280 281 conn.Open(); 282 o = cmd.ExecuteScalar(); 283 conn.Close(); 284 } 285 return o; 286 287 } 288 289 290 291 #endregion 292 293 294 295 296 297 #region 查询获取DataReader------------------------------------ 298 299 300 301 302 /// <summary> 303 /// 调用不带参数的存储过程,返回DataReader对象 304 /// </summary> 305 /// <param name="procName">存储过程名称</param> 306 /// <returns>DataReader对象</returns> 307 public static SqlDataReader GetReaderByProc(string procName) 308 { 309 return GetReaderByProc(procName, null); 310 } 311 /// <summary> 312 /// 调用带有参数的存储过程,返回DataReader对象 313 /// </summary> 314 /// <param name="procName">存储过程名</param> 315 /// <param name="paras">参数数组</param> 316 /// <returns>DataReader对象</returns> 317 public static SqlDataReader GetReaderByProc(string procName, SqlParameter[] paras) 318 { 319 return GetReader(procName, paras, CommandType.StoredProcedure); 320 } 321 /// <summary> 322 /// 根据sql语句返回DataReader对象 323 /// </summary> 324 /// <param name="strSQL">sql语句</param> 325 /// <returns>DataReader对象</returns> 326 public static SqlDataReader GetReader(string strSQL) 327 { 328 return GetReader(strSQL, null); 329 } 330 /// <summary> 331 /// 根据sql语句和参数返回DataReader对象 332 /// </summary> 333 /// <param name="strSQL">sql语句</param> 334 /// <param name="paras">参数数组</param> 335 /// <returns>DataReader对象</returns> 336 public static SqlDataReader GetReader(string strSQL, SqlParameter[] paras) 337 { 338 return GetReader(strSQL, paras, CommandType.Text); 339 } 340 /// <summary> 341 /// 查询SQL语句获取DataReader 342 /// </summary> 343 /// <param name="strSQL">查询的SQL语句</param> 344 /// <param name="paras">参数列表,没有参数填入null</param> 345 /// <returns>查询到的DataReader(关闭该对象的时候,自动关闭连接)</returns> 346 public static SqlDataReader GetReader(string strSQL, SqlParameter[] paras, CommandType cmdtype) 347 { 348 SqlDataReader sqldr = null; 349 SqlConnection conn = new SqlConnection(strConn); 350 SqlCommand cmd = new SqlCommand(strSQL, conn); 351 cmd.CommandType = cmdtype; 352 if (paras != null) 353 { 354 cmd.Parameters.AddRange(paras); 355 } 356 conn.Open(); 357 //CommandBehavior.CloseConnection的作用是如果关联的DataReader对象关闭,则连接自动关闭 358 sqldr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 359 return sqldr; 360 } 361 362 363 364 #endregion 365 366 367 368 369 #region 批量插入数据--------------------------------------------- 370 371 372 373 374 /// <summary> 375 /// 往数据库中批量插入数据 376 /// </summary> 377 /// <param name="sourceDt">数据源表</param> 378 /// <param name="targetTable">服务器上目标表</param> 379 public static void BulkToDB(DataTable sourceDt, string targetTable) 380 { 381 SqlConnection conn = new SqlConnection(strConn); 382 SqlBulkCopy bulkCopy = new SqlBulkCopy(conn); //用其它源的数据有效批量加载sql server表中 383 bulkCopy.DestinationTableName = targetTable; //服务器上目标表的名称 384 bulkCopy.BatchSize = sourceDt.Rows.Count; //每一批次中的行数 385 386 try 387 { 388 conn.Open(); 389 if (sourceDt != null && sourceDt.Rows.Count != 0) 390 bulkCopy.WriteToServer(sourceDt); //将提供的数据源中的所有行复制到目标表中 391 } 392 catch (Exception ex) 393 { 394 throw ex; 395 } 396 finally 397 { 398 conn.Close(); 399 if (bulkCopy != null) 400 bulkCopy.Close(); 401 } 402 403 } 404 405 #endregion 406 407 408 } 409 }
2015-1-27 22:32:35
发现asp.net每个页面在创建的时候vs都会自动生成一个表单,有时候我们希望通过enter键来提交表单,因为用户为了方便经常这样做。在asp.net中可以设置任意一个按钮为enter键提交。设置方法是直接在表单里面加入这一句话<form id="form1" runat="server" defaultbutton="ImageButton2">这里的defaultbutton就是默认提交按钮
http://blog.sina.com.cn/s/blog_4acf507301011ds5.html
http://blog.csdn.net/sunbett/article/details/6496843
搜索关键字:asp.net textbox 按enter请求到后台代码