FoolWeb 各层代码实例

未命名

FoolWeb.DateEngine 数据库操作类库

这里现在只写了一个类用于操作mssql.将来支持别的数据库试直接扩展就行了.

下来带个代码段

   1: /// <summary>通用数据库接口
   2: /// </summary>
   3: using System;
   4: using System.Collections;
   5: using System.Collections.Generic;
   6: using System.Data;
   7: using System.Data.SqlClient;
   8: using System.Configuration;
   9:  
  10: namespace FoolWeb.DateEngine {
  11:  
  12:     #region 事务属性
  13:     public enum EffentNextType {
  14:         /// <summary>
  15:         /// 对其他语句无任何影响 
  16:         /// </summary>
  17:         None,
  18:         /// <summary>
  19:         /// 当前语句必须为"SELECT COUNT(1) FROM .."格式,如果存在则继续执行,不存在回滚事务
  20:         /// </summary>
  21:         WhenHaveContine,
  22:         /// <summary>             
  23:         /// 当前语句必须为"SELECT COUNT(1) FROM .."格式,如果不存在则继续执行,存在回滚事务
  24:         /// </summary>
  25:         WhenNoHaveContine,
  26:         /// <summary>
  27:         /// 当前语句影响到的行数必须大于0,否则回滚事务
  28:         /// </summary>
  29:         ExcuteEffectRows,
  30:         /// <summary>
  31:         /// 引发事件-当前语句必须为"SELECT COUNT(1) FROM .."格式,如果不存在则继续执行,存在回滚事务
  32:         /// </summary>
  33:         SolicitationEvent
  34:     }
  35:  
  36:     #endregion
  37:     #region command定义
  38:     public class CommandInfo {
  39:         public object ShareObject=null;
  40:         public object OriginalData=null;
  41:         event EventHandler _solicitationEvent;
  42:  
  43:         public event EventHandler SolicitationEvent {
  44:             add {
  45:                 _solicitationEvent+=value;
  46:             }
  47:             remove {
  48:                 _solicitationEvent-=value;
  49:             }
  50:         }
  51:  
  52:         public void OnSolicitationEvent() {
  53:             if( _solicitationEvent!=null ) {
  54:                 _solicitationEvent(this, new EventArgs());
  55:             }
  56:         }
  57:  
  58:         public string CommandText;
  59:         public System.Data.Common.DbParameter[] Parameters;
  60:         public EffentNextType EffentNextType=EffentNextType.None;
  61:  
  62:         public CommandInfo() {
  63:         }
  64:  
  65:         public CommandInfo( string sqlText, SqlParameter[] para ) {
  66:             this.CommandText=sqlText;
  67:             this.Parameters=para;
  68:         }
  69:  
  70:         public CommandInfo( string sqlText, SqlParameter[] para, EffentNextType type ) {
  71:             this.CommandText=sqlText;
  72:             this.Parameters=para;
  73:             this.EffentNextType=type;
  74:         }
  75:     }
  76:     #endregion    
  77:     /// <summary>
  78:     /// 数据访问抽象基础类 
  79:     /// </summary>
  80:     public abstract class MsSQLHelper {
  81:         #region 属性定义
  82:         //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
  83:         public static string connectionString="";
  84:         //构造函数默认数据库
  85:         public MsSQLHelper() {
  86:             connectionString=ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  87:         }
  88:         //构造函数指定数据库
  89:         public MsSQLHelper( string _connstr ) {
  90:             connectionString=ConfigurationManager.ConnectionStrings[_connstr].ConnectionString;
  91:         }
  92:         #endregion       
  93:         #region 公用方法
  94:         /// <summary>
  95:         /// 判断是否存在某表的某个字段
  96:         /// </summary>
  97:         /// <param name="tableName">表名称</param>
  98:         /// <param name="columnName">列名称</param>
  99:         /// <returns>是否存在</returns>
 100:         public static bool ColumnExists( string tableName, string columnName ) {
 101:             string sql="select count(1) from syscolumns where [id]=object_id(\""+tableName+"\") and [name]=\""+columnName+"\"";
 102:             object res=GetSingle(sql);
 103:             if( res==null ) {
 104:                 return false;
 105:             }
 106:             return Convert.ToInt32(res)>0;
 107:         }
 108:         /// <summary>
 109:         /// 获取表中数字字段的最大值
 110:         /// </summary>
 111:         /// <param name="FieldName">字段名</param>
 112:         /// <param name="TableName">表名</param>
 113:         /// <returns>返回字段最大值max(空的话返回0)</returns>
 114:         public static int GetMaxID( string FieldName, string TableName ) {
 115:             string strsql="select max("+FieldName+") from "+TableName;
 116:             object obj=MsSQLHelper.GetSingle(strsql);
 117:             if( obj==null ) {
 118:                 return 0;
 119:             } else {
 120:                 return int.Parse(obj.ToString());
 121:             }
 122:         }
 123:         /// <summary>
 124:         /// 判断知否存在值
 125:         /// </summary>
 126:         /// <param name="strSql">sql语句</param>
 127:         /// <returns>存在true不存在false</returns>
 128:         public static bool Exists( string strSql ) {
 129:             object obj=MsSQLHelper.GetSingle(strSql);
 130:             int cmdresult;
 131:             if( ( Object.Equals(obj, null) )||( Object.Equals(obj, System.DBNull.Value) ) ) {
 132:                 cmdresult=0;
 133:             } else {
 134:                 cmdresult=int.Parse(obj.ToString());
 135:             }
 136:             if( cmdresult==0 ) {
 137:                 return false;
 138:             } else {
 139:                 return true;
 140:             }
 141:         }
 142:         /// <summary>
 143:         /// 表是否存在
 144:         /// </summary>
 145:         /// <param name="TableName">表名</param>
 146:         /// <returns>存在true不存在false</returns>
 147:         public static bool TabExists( string TableName ) {
 148:             string strsql="select count(*) from sysobjects where id = object_id(N\"["+TableName+"]\") and OBJECTPROPERTY(id, N\"IsUserTable\") = 1";
 149:             object obj=MsSQLHelper.GetSingle(strsql);
 150:             int cmdresult;
 151:             if( ( Object.Equals(obj, null) )||( Object.Equals(obj, System.DBNull.Value) ) ) {
 152:                 cmdresult=0;
 153:             } else {
 154:                 cmdresult=int.Parse(obj.ToString());
 155:             }
 156:             if( cmdresult==0 ) {
 157:                 return false;
 158:             } else {
 159:                 return true;
 160:             }
 161:         }
 162:         public static bool Exists( string strSql, params SqlParameter[] cmdParms ) {
 163:             object obj=MsSQLHelper.GetSingle(strSql, cmdParms);
 164:             int cmdresult;
 165:             if( ( Object.Equals(obj, null) )||( Object.Equals(obj, System.DBNull.Value) ) ) {
 166:                 cmdresult=0;
 167:             } else {
 168:                 cmdresult=int.Parse(obj.ToString());
 169:             }
 170:             if( cmdresult==0 ) {
 171:                 return false;
 172:             } else {
 173:                 return true;
 174:             }
 175:         }
 176:         #endregion
 177:         #region 执行简单SQL语句
 178:         /// <summary>
 179:         /// 执行SQL语句,返回影响的记录数
 180:         /// </summary>
 181:         /// <param name="SQLString">SQL语句</param>
 182:         /// <returns>影响的记录数</returns>
 183:         public static int ExecuteSql( string SQLString ) {
 184:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 185:                 using( SqlCommand cmd=new SqlCommand(SQLString, connection) ) {
 186:                     try {
 187:                         connection.Open();
 188:                         int rows=cmd.ExecuteNonQuery();
 189:                         return rows;
 190:                     } catch( System.Data.SqlClient.SqlException e ) {
 191:                         connection.Close();
 192:                         throw e;
 193:                     }
 194:                 }
 195:             }
 196:         }
 197:         /// <summary>
 198:         /// 执行SQL语句,返回影响的记录数(延迟执行)
 199:         /// </summary>
 200:         /// <param name="SQLString">SQL语句</param>
 201:         /// <param name="Times">延迟时间</param>
 202:         /// <returns>影响的记录数</returns>
 203:         public static int ExecuteSqlByTime( string SQLString, int Times ) {
 204:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 205:                 using( SqlCommand cmd=new SqlCommand(SQLString, connection) ) {
 206:                     try {
 207:                         connection.Open();
 208:                         cmd.CommandTimeout=Times;
 209:                         int rows=cmd.ExecuteNonQuery();
 210:                         return rows;
 211:                     } catch( System.Data.SqlClient.SqlException e ) {
 212:                         connection.Close();
 213:                         throw e;
 214:                     }
 215:                 }
 216:             }
 217:         }
 218:  
 219:         /// <summary>
 220:         /// 执行多条SQL语句,实现数据库事务。
 221:         /// </summary>
 222:         /// <param name="SQLStringList">多条SQL语句</param>
 223:         /// <returns>影响的记录数</returns>
 224:         public static int ExecuteSqlTran( List<String> SQLStringList ) {
 225:             using( SqlConnection conn=new SqlConnection(connectionString) ) {
 226:                 conn.Open();
 227:                 SqlCommand cmd=new SqlCommand();
 228:                 cmd.Connection=conn;
 229:                 SqlTransaction tx=conn.BeginTransaction();
 230:                 cmd.Transaction=tx;
 231:                 try {
 232:                     int count=0;
 233:                     for( int n=0 ; n<SQLStringList.Count ; n++ ) {
 234:                         string strsql=SQLStringList[n];
 235:                         if( strsql.Trim().Length>1 ) {
 236:                             cmd.CommandText=strsql;
 237:                             count+=cmd.ExecuteNonQuery();
 238:                         }
 239:                     }
 240:                     tx.Commit();
 241:                     return count;
 242:                 } catch {
 243:                     tx.Rollback();
 244:                     return 0;
 245:                 }
 246:             }
 247:         }
 248:         /// <summary>
 249:         /// 执行带一个存储过程参数的的SQL语句。
 250:         /// </summary>
 251:         /// <param name="SQLString">SQL语句</param>
 252:         /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
 253:         /// <returns>影响的记录数</returns>
 254:         public static int ExecuteSql( string SQLString, string content ) {
 255:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 256:                 SqlCommand cmd=new SqlCommand(SQLString, connection);
 257:                 System.Data.SqlClient.SqlParameter myParameter=new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
 258:                 myParameter.Value=content;
 259:                 cmd.Parameters.Add(myParameter);
 260:                 try {
 261:                     connection.Open();
 262:                     int rows=cmd.ExecuteNonQuery();
 263:                     return rows;
 264:                 } catch( System.Data.SqlClient.SqlException e ) {
 265:                     throw e;
 266:                 } finally {
 267:                     cmd.Dispose();
 268:                     connection.Close();
 269:                 }
 270:             }
 271:         }
 272:         /// <summary>
 273:         /// 执行带一个存储过程参数的的SQL语句。
 274:         /// </summary>
 275:         /// <param name="SQLString">SQL语句</param>
 276:         /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
 277:         /// <returns>返回查询结果第一列</returns>
 278:         public static object ExecuteSqlGet( string SQLString, string content ) {
 279:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 280:                 SqlCommand cmd=new SqlCommand(SQLString, connection);
 281:                 System.Data.SqlClient.SqlParameter myParameter=new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
 282:                 myParameter.Value=content;
 283:                 cmd.Parameters.Add(myParameter);
 284:                 try {
 285:                     connection.Open();
 286:                     object obj=cmd.ExecuteScalar();
 287:                     if( ( Object.Equals(obj, null) )||( Object.Equals(obj, System.DBNull.Value) ) ) {
 288:                         return null;
 289:                     } else {
 290:                         return obj;
 291:                     }
 292:                 } catch( System.Data.SqlClient.SqlException e ) {
 293:                     throw e;
 294:                 } finally {
 295:                     cmd.Dispose();
 296:                     connection.Close();
 297:                 }
 298:             }
 299:         }
 300:         /// <summary>
 301:         /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
 302:         /// </summary>
 303:         /// <param name="strSQL">SQL语句</param>
 304:         /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
 305:         /// <returns>影响的记录数</returns>
 306:         public static int ExecuteSqlInsertImg( string strSQL, byte[] fs ) {
 307:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 308:                 SqlCommand cmd=new SqlCommand(strSQL, connection);
 309:                 System.Data.SqlClient.SqlParameter myParameter=new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image);
 310:                 myParameter.Value=fs;
 311:                 cmd.Parameters.Add(myParameter);
 312:                 try {
 313:                     connection.Open();
 314:                     int rows=cmd.ExecuteNonQuery();
 315:                     return rows;
 316:                 } catch( System.Data.SqlClient.SqlException e ) {
 317:                     throw e;
 318:                 } finally {
 319:                     cmd.Dispose();
 320:                     connection.Close();
 321:                 }
 322:             }
 323:         }
 324:         /// <summary>
 325:         /// 执行一条计算查询结果语句,返回查询结果(object)。
 326:         /// </summary>
 327:         /// <param name="SQLString">计算查询结果语句</param>
 328:         /// <returns>查询结果(object)</returns>
 329:         public static object GetSingle( string SQLString ) {
 330:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 331:                 using( SqlCommand cmd=new SqlCommand(SQLString, connection) ) {
 332:                     try {
 333:                         connection.Open();
 334:                         object obj=cmd.ExecuteScalar();
 335:                         if( ( Object.Equals(obj, null) )||( Object.Equals(obj, System.DBNull.Value) ) ) {
 336:                             return null;
 337:                         } else {
 338:                             return obj;
 339:                         }
 340:                     } catch( System.Data.SqlClient.SqlException e ) {
 341:                         connection.Close();
 342:                         throw e;
 343:                     }
 344:                 }
 345:             }
 346:         }
 347:         public static object GetSingle( string SQLString, int Times ) {
 348:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 349:                 using( SqlCommand cmd=new SqlCommand(SQLString, connection) ) {
 350:                     try {
 351:                         connection.Open();
 352:                         cmd.CommandTimeout=Times;
 353:                         object obj=cmd.ExecuteScalar();
 354:                         if( ( Object.Equals(obj, null) )||( Object.Equals(obj, System.DBNull.Value) ) ) {
 355:                             return null;
 356:                         } else {
 357:                             return obj;
 358:                         }
 359:                     } catch( System.Data.SqlClient.SqlException e ) {
 360:                         connection.Close();
 361:                         throw e;
 362:                     }
 363:                 }
 364:             }
 365:         }
 366:         /// <summary>
 367:         /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
 368:         /// </summary>
 369:         /// <param name="strSQL">查询语句</param>
 370:         /// <returns>SqlDataReader</returns>
 371:         public static SqlDataReader ExecuteReader( string strSQL ) {
 372:             SqlConnection connection=new SqlConnection(connectionString);
 373:             SqlCommand cmd=new SqlCommand(strSQL, connection);
 374:             try {
 375:                 connection.Open();
 376:                 SqlDataReader myReader=cmd.ExecuteReader(CommandBehavior.CloseConnection);
 377:                 return myReader;
 378:             } catch( System.Data.SqlClient.SqlException e ) {
 379:                 throw e;
 380:             }
 381:         }
 382:         /// <summary>
 383:         /// 执行查询语句,返回DataSet
 384:         /// </summary>
 385:         /// <param name="SQLString">查询语句</param>
 386:         /// <returns>DataSet</returns>
 387:         public static DataSet Query( string SQLString ) {
 388:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 389:                 DataSet ds=new DataSet();
 390:                 try {
 391:                     connection.Open();
 392:                     SqlDataAdapter command=new SqlDataAdapter(SQLString, connection);
 393:                     command.Fill(ds, "ds");
 394:                 } catch( System.Data.SqlClient.SqlException ex ) {
 395:                     throw new Exception(ex.Message);
 396:                 }
 397:                 return ds;
 398:             }
 399:         }
 400:         /// <summary>
 401:         /// 查询并得到数据集DataSet
 402:         /// </summary>
 403:         /// <param name="SQLString">查询语句</param>
 404:         /// <param name="Times"></param>
 405:         /// <returns></returns>
 406:         public static DataSet Query( string SQLString, int Times ) {
 407:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 408:                 DataSet ds=new DataSet();
 409:                 try {
 410:                     connection.Open();
 411:                     SqlDataAdapter command=new SqlDataAdapter(SQLString, connection);
 412:                     command.SelectCommand.CommandTimeout=Times;
 413:                     command.Fill(ds, "ds");
 414:                 } catch( System.Data.SqlClient.SqlException ex ) {
 415:                     throw new Exception(ex.Message);
 416:                 }
 417:                 return ds;
 418:             }
 419:         }
 420:         #endregion
 421:         #region 执行带参数的SQL语句
 422:         /// <summary>
 423:         /// 执行SQL语句,返回影响的记录数
 424:         /// </summary>
 425:         /// <param name="SQLString">SQL语句</param>
 426:         /// <returns>影响的记录数</returns>
 427:         public static int ExecuteSql( string SQLString, params SqlParameter[] cmdParms ) {
 428:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 429:                 using( SqlCommand cmd=new SqlCommand() ) {
 430:                     try {
 431:                         PrepareCommand(cmd, connection, null, SQLString, cmdParms);
 432:                         int rows=cmd.ExecuteNonQuery();
 433:                         cmd.Parameters.Clear();
 434:                         return rows;
 435:                     } catch( System.Data.SqlClient.SqlException e ) {
 436:                         throw e;
 437:                     }
 438:                 }
 439:             }
 440:         }
 441:         /// <summary>
 442:         /// 执行多条SQL语句,实现数据库事务。
 443:         /// </summary>
 444:         /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
 445:         public static void ExecuteSqlTran( Hashtable SQLStringList ) {
 446:             using( SqlConnection conn=new SqlConnection(connectionString) ) {
 447:                 conn.Open();
 448:                 using( SqlTransaction trans=conn.BeginTransaction() ) {
 449:                     SqlCommand cmd=new SqlCommand();
 450:                     try {
 451:                         //循环
 452:                         foreach( DictionaryEntry myDE in SQLStringList ) {
 453:                             string cmdText=myDE.Key.ToString();
 454:                             SqlParameter[] cmdParms=(SqlParameter[])myDE.Value;
 455:                             PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
 456:                             int val=cmd.ExecuteNonQuery();
 457:                             cmd.Parameters.Clear();
 458:                         }
 459:                         trans.Commit();
 460:                     } catch {
 461:                         trans.Rollback();
 462:                         throw;
 463:                     }
 464:                 }
 465:             }
 466:         }
 467:         /// <summary>
 468:         /// 执行多条SQL语句,实现数据库事务。
 469:         /// </summary>
 470:         /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
 471:         public static int ExecuteSqlTran( System.Collections.Generic.List<CommandInfo> cmdList ) {
 472:             using( SqlConnection conn=new SqlConnection(connectionString) ) {
 473:                 conn.Open();
 474:                 using( SqlTransaction trans=conn.BeginTransaction() ) {
 475:                     SqlCommand cmd=new SqlCommand();
 476:                     try {
 477:                         int count=0;
 478:                         //循环
 479:                         foreach( CommandInfo myDE in cmdList ) {
 480:                             string cmdText=myDE.CommandText;
 481:                             SqlParameter[] cmdParms=(SqlParameter[])myDE.Parameters;
 482:                             PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
 483:                             if( myDE.EffentNextType==EffentNextType.WhenHaveContine||myDE.EffentNextType==EffentNextType.WhenNoHaveContine ) {
 484:                                 if( myDE.CommandText.ToLower().IndexOf("count(")==-1 ) {
 485:                                     trans.Rollback();
 486:                                     return 0;
 487:                                 }
 488:                                 object obj=cmd.ExecuteScalar();
 489:                                 bool isHave=false;
 490:                                 if( obj==null&&obj==DBNull.Value ) {
 491:                                     isHave=false;
 492:                                 }
 493:                                 isHave=Convert.ToInt32(obj)>0;
 494:                                 if( myDE.EffentNextType==EffentNextType.WhenHaveContine&&!isHave ) {
 495:                                     trans.Rollback();
 496:                                     return 0;
 497:                                 }
 498:                                 if( myDE.EffentNextType==EffentNextType.WhenNoHaveContine&&isHave ) {
 499:                                     trans.Rollback();
 500:                                     return 0;
 501:                                 }
 502:                                 continue;
 503:                             }
 504:                             int val=cmd.ExecuteNonQuery();
 505:                             count+=val;
 506:                             if( myDE.EffentNextType==EffentNextType.ExcuteEffectRows&&val==0 ) {
 507:                                 trans.Rollback();
 508:                                 return 0;
 509:                             }
 510:                             cmd.Parameters.Clear();
 511:                         }
 512:                         trans.Commit();
 513:                         return count;
 514:                     } catch {
 515:                         trans.Rollback();
 516:                         throw;
 517:                     }
 518:                 }
 519:             }
 520:         }
 521:         /// <summary>
 522:         /// 执行多条SQL语句,实现数据库事务。
 523:         /// </summary>
 524:         /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
 525:         public static void ExecuteSqlTranWithIndentity( System.Collections.Generic.List<CommandInfo> SQLStringList ) {
 526:             using( SqlConnection conn=new SqlConnection(connectionString) ) {
 527:                 conn.Open();
 528:                 using( SqlTransaction trans=conn.BeginTransaction() ) {
 529:                     SqlCommand cmd=new SqlCommand();
 530:                     try {
 531:                         int indentity=0;
 532:                         //循环
 533:                         foreach( CommandInfo myDE in SQLStringList ) {
 534:                             string cmdText=myDE.CommandText;
 535:                             SqlParameter[] cmdParms=(SqlParameter[])myDE.Parameters;
 536:                             foreach( SqlParameter q in cmdParms ) {
 537:                                 if( q.Direction==ParameterDirection.InputOutput ) {
 538:                                     q.Value=indentity;
 539:                                 }
 540:                             }
 541:                             PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
 542:                             int val=cmd.ExecuteNonQuery();
 543:                             foreach( SqlParameter q in cmdParms ) {
 544:                                 if( q.Direction==ParameterDirection.Output ) {
 545:                                     indentity=Convert.ToInt32(q.Value);
 546:                                 }
 547:                             }
 548:                             cmd.Parameters.Clear();
 549:                         }
 550:                         trans.Commit();
 551:                     } catch {
 552:                         trans.Rollback();
 553:                         throw;
 554:                     }
 555:                 }
 556:             }
 557:         }
 558:         /// <summary>
 559:         /// 执行多条SQL语句,实现数据库事务。
 560:         /// </summary>
 561:         /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
 562:         public static void ExecuteSqlTranWithIndentity( Hashtable SQLStringList ) {
 563:             using( SqlConnection conn=new SqlConnection(connectionString) ) {
 564:                 conn.Open();
 565:                 using( SqlTransaction trans=conn.BeginTransaction() ) {
 566:                     SqlCommand cmd=new SqlCommand();
 567:                     try {
 568:                         int indentity=0;
 569:                         //循环
 570:                         foreach( DictionaryEntry myDE in SQLStringList ) {
 571:                             string cmdText=myDE.Key.ToString();
 572:                             SqlParameter[] cmdParms=(SqlParameter[])myDE.Value;
 573:                             foreach( SqlParameter q in cmdParms ) {
 574:                                 if( q.Direction==ParameterDirection.InputOutput ) {
 575:                                     q.Value=indentity;
 576:                                 }
 577:                             }
 578:                             PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
 579:                             int val=cmd.ExecuteNonQuery();
 580:                             foreach( SqlParameter q in cmdParms ) {
 581:                                 if( q.Direction==ParameterDirection.Output ) {
 582:                                     indentity=Convert.ToInt32(q.Value);
 583:                                 }
 584:                             }
 585:                             cmd.Parameters.Clear();
 586:                         }
 587:                         trans.Commit();
 588:                     } catch {
 589:                         trans.Rollback();
 590:                         throw;
 591:                     }
 592:                 }
 593:             }
 594:         }
 595:         /// <summary>
 596:         /// 执行一条计算查询结果语句,返回查询结果(object)。
 597:         /// </summary>
 598:         /// <param name="SQLString">计算查询结果语句</param>
 599:         /// <returns>查询结果(object)</returns>
 600:         public static object GetSingle( string SQLString, params SqlParameter[] cmdParms ) {
 601:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 602:                 using( SqlCommand cmd=new SqlCommand() ) {
 603:                     try {
 604:                         PrepareCommand(cmd, connection, null, SQLString, cmdParms);
 605:                         object obj=cmd.ExecuteScalar();
 606:                         cmd.Parameters.Clear();
 607:                         if( ( Object.Equals(obj, null) )||( Object.Equals(obj, System.DBNull.Value) ) ) {
 608:                             return null;
 609:                         } else {
 610:                             return obj;
 611:                         }
 612:                     } catch( System.Data.SqlClient.SqlException e ) {
 613:                         throw e;
 614:                     }
 615:                 }
 616:             }
 617:         }
 618:         /// <summary>
 619:         /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
 620:         /// </summary>
 621:         /// <param name="strSQL">查询语句</param>
 622:         /// <returns>SqlDataReader</returns>
 623:         public static SqlDataReader ExecuteReader( string SQLString, params SqlParameter[] cmdParms ) {
 624:             SqlConnection connection=new SqlConnection(connectionString);
 625:             SqlCommand cmd=new SqlCommand();
 626:             try {
 627:                 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
 628:                 SqlDataReader myReader=cmd.ExecuteReader(CommandBehavior.CloseConnection);
 629:                 cmd.Parameters.Clear();
 630:                 return myReader;
 631:             } catch( System.Data.SqlClient.SqlException e ) {
 632:                 throw e;
 633:             }
 634:         }
 635:         /// <summary>
 636:         /// 执行查询语句,返回DataSet
 637:         /// </summary>
 638:         /// <param name="SQLString">查询语句</param>
 639:         /// <returns>DataSet</returns>
 640:         public static DataSet Query( string SQLString, params SqlParameter[] cmdParms ) {
 641:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 642:                 SqlCommand cmd=new SqlCommand();
 643:                 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
 644:                 using( SqlDataAdapter da=new SqlDataAdapter(cmd) ) {
 645:                     DataSet ds=new DataSet();
 646:                     try {
 647:                         da.Fill(ds, "ds");
 648:                         cmd.Parameters.Clear();
 649:                     } catch( System.Data.SqlClient.SqlException ex ) {
 650:                         throw new Exception(ex.Message);
 651:                     }
 652:                     return ds;
 653:                 }
 654:             }
 655:         }
 656:         private static void PrepareCommand( SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms ) {
 657:             if( conn.State!=ConnectionState.Open )
 658:                 conn.Open();
 659:             cmd.Connection=conn;
 660:             cmd.CommandText=cmdText;
 661:             if( trans!=null )
 662:                 cmd.Transaction=trans;
 663:             cmd.CommandType=CommandType.Text;//cmdType;
 664:             if( cmdParms!=null ) {
 665:                 foreach( SqlParameter parameter in cmdParms ) {
 666:                     if( ( parameter.Direction==ParameterDirection.InputOutput||parameter.Direction==ParameterDirection.Input )&&
 667:                     ( parameter.Value==null ) ) {
 668:                         parameter.Value=DBNull.Value;
 669:                     }
 670:                     cmd.Parameters.Add(parameter);
 671:                 }
 672:             }
 673:         }
 674:         #endregion
 675:         #region 存储过程操作
 676:         /// <summary>
 677:         /// 执行存储过程,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
 678:         /// </summary>
 679:         /// <param name="storedProcName">存储过程名</param>
 680:         /// <returns>SqlDataReader</returns>
 681:         public static SqlDataReader RunProcedure( string storedProcName ) {
 682:             SqlConnection connection=new SqlConnection(connectionString);
 683:             SqlDataReader returnReader;
 684:             connection.Open();
 685:             SqlCommand command=new SqlCommand(storedProcName, connection);
 686:             command.CommandType=CommandType.StoredProcedure;
 687:             returnReader=command.ExecuteReader(CommandBehavior.CloseConnection);
 688:             return returnReader;
 689:         }
 690:  
 691:         /// <summary>
 692:         /// 执行存储过程,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
 693:         /// </summary>
 694:         /// <param name="storedProcName">存储过程名</param>
 695:         /// <param name="parameters">存储过程参数</param>
 696:         /// <returns>SqlDataReader</returns>
 697:         public static SqlDataReader RunProcedure( string storedProcName, IDataParameter[] parameters ) {
 698:             SqlConnection connection=new SqlConnection(connectionString);
 699:             SqlDataReader returnReader;
 700:             connection.Open();
 701:             SqlCommand command=BuildQueryCommand(connection, storedProcName, parameters);
 702:             command.CommandType=CommandType.StoredProcedure;
 703:             returnReader=command.ExecuteReader(CommandBehavior.CloseConnection);
 704:             return returnReader;
 705:         }
 706:         /// <summary>
 707:         /// 执行存储过程
 708:         /// </summary>
 709:         /// <param name="storedProcName">存储过程名</param>
 710:         /// <param name="parameters">存储过程参数</param>
 711:         /// <param name="tableName">DataSet结果中的表名</param>
 712:         /// <returns>DataSet</returns>
 713:         public static DataSet RunProcedure( string storedProcName, string tableName ) {
 714:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 715:                 DataSet dataSet=new DataSet();
 716:                 connection.Open();
 717:                 SqlDataAdapter sqlDA=new SqlDataAdapter();
 718:                 sqlDA.SelectCommand=new SqlCommand(storedProcName, connection);
 719:                 sqlDA.Fill(dataSet, tableName);
 720:                 connection.Close();
 721:                 return dataSet;
 722:             }
 723:         }
 724:         /// <summary>
 725:         /// 执行存储过程
 726:         /// </summary>
 727:         /// <param name="storedProcName">存储过程名</param>
 728:         /// <param name="parameters">存储过程参数</param>
 729:         /// <param name="tableName">DataSet结果中的表名</param>
 730:         /// <returns>DataSet</returns>
 731:         public static DataSet RunProcedure( string storedProcName, IDataParameter[] parameters, string tableName ) {
 732:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 733:                 DataSet dataSet=new DataSet();
 734:                 connection.Open();
 735:                 SqlDataAdapter sqlDA=new SqlDataAdapter();
 736:                 sqlDA.SelectCommand=BuildQueryCommand(connection, storedProcName, parameters);
 737:                 sqlDA.Fill(dataSet, tableName);
 738:                 connection.Close();
 739:                 return dataSet;
 740:             }
 741:         }
 742:         public static DataSet RunProcedure( string storedProcName, IDataParameter[] parameters, string tableName, int Times ) {
 743:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 744:                 DataSet dataSet=new DataSet();
 745:                 connection.Open();
 746:                 SqlDataAdapter sqlDA=new SqlDataAdapter();
 747:                 sqlDA.SelectCommand=BuildQueryCommand(connection, storedProcName, parameters);
 748:                 sqlDA.SelectCommand.CommandTimeout=Times;
 749:                 sqlDA.Fill(dataSet, tableName);
 750:                 connection.Close();
 751:                 return dataSet;
 752:             }
 753:         }
 754:         /// <summary>
 755:         /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
 756:         /// </summary>
 757:         /// <param name="connection">数据库连接</param>
 758:         /// <param name="storedProcName">存储过程名</param>
 759:         /// <param name="parameters">存储过程参数</param>
 760:         /// <returns>SqlCommand</returns>
 761:         private static SqlCommand BuildQueryCommand( SqlConnection connection, string storedProcName, IDataParameter[] parameters ) {
 762:             SqlCommand command=new SqlCommand(storedProcName, connection);
 763:             command.CommandType=CommandType.StoredProcedure;
 764:             foreach( SqlParameter parameter in parameters ) {
 765:                 if( parameter!=null ) {
 766:                     // 检查未分配值的输出参数,将其分配以DBNull.Value.
 767:                     if( ( parameter.Direction==ParameterDirection.InputOutput||parameter.Direction==ParameterDirection.Input )&&
 768:                     ( parameter.Value==null ) ) {
 769:                         parameter.Value=DBNull.Value;
 770:                     }
 771:                     command.Parameters.Add(parameter);
 772:                 }
 773:             }
 774:             return command;
 775:         }
 776:         /// <summary>
 777:         /// 执行存储过程,返回影响的行数
 778:         /// </summary>
 779:         /// <param name="storedProcName">存储过程名</param>
 780:         /// <param name="parameters">存储过程参数</param>
 781:         /// <param name="rowsAffected">影响的行数</param>
 782:         /// <returns></returns>
 783:         public static int RunProcedure( string storedProcName, out int rowsAffected ) {
 784:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 785:                 int result;
 786:                 connection.Open();
 787:                 SqlCommand command=new SqlCommand(storedProcName, connection);
 788:                 rowsAffected=command.ExecuteNonQuery();
 789:                 result=(int)command.Parameters["ReturnValue"].Value;
 790:                 //Connection.Close();
 791:                 return result;
 792:             }
 793:         }
 794:         /// <summary>
 795:         /// 执行存储过程,返回影响的行数
 796:         /// </summary>
 797:         /// <param name="storedProcName">存储过程名</param>
 798:         /// <param name="parameters">存储过程参数</param>
 799:         /// <param name="rowsAffected">影响的行数</param>
 800:         /// <returns></returns>
 801:         public static int RunProcedure( string storedProcName, IDataParameter[] parameters, out int rowsAffected ) {
 802:             using( SqlConnection connection=new SqlConnection(connectionString) ) {
 803:                 int result;
 804:                 connection.Open();
 805:                 SqlCommand command=BuildIntCommand(connection, storedProcName, parameters);
 806:                 rowsAffected=command.ExecuteNonQuery();
 807:                 result=(int)command.Parameters["ReturnValue"].Value;
 808:                 //Connection.Close();
 809:                 return result;
 810:             }
 811:         }
 812:         /// <summary>
 813:         /// 创建 SqlCommand 对象实例(用来返回一个整数值)
 814:         /// </summary>
 815:         /// <param name="storedProcName">存储过程名</param>
 816:         /// <param name="parameters">存储过程参数</param>
 817:         /// <returns>SqlCommand 对象实例</returns>
 818:         private static SqlCommand BuildIntCommand( SqlConnection connection, string storedProcName, IDataParameter[] parameters ) {
 819:             SqlCommand command=BuildQueryCommand(connection, storedProcName, parameters);
 820:             command.Parameters.Add(new SqlParameter("ReturnValue",
 821:             SqlDbType.Int, 4, ParameterDirection.ReturnValue,
 822:             false, 0, 0, string.Empty, DataRowVersion.Default, null));
 823:             return command;
 824:         }
 825:         #endregion
 826:     }
 827: }

转载于:https://www.cnblogs.com/binghe875/archive/2011/11/21/2257399.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/495221.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

google、bing、baidu、shodan、censys、ZoomEye 搜索引擎 高级用法

Google guide &#xff1a;http://www.googleguide.com/advanced_operators_reference.html 相关资料&#xff1a; &#xff1a;http://www.bubuko.com/infodetail-2292041.html&#xff1a;http://www.pinginglab.net/open/course/9&#xff1a;https://download.csdn.net/d…

南云等PNAS研究论文:揭示儿童音乐学习向语言领域迁移的脑机制

来源&#xff1a;神经科技摘要&#xff1a;南云等的研究结果表明&#xff0c;半年的钢琴训练提升了4到5岁儿童大脑对跨音乐与语言领域的音高变化的敏感性2018年6月25日&#xff0c;美国科学院院刊PNAS在线发表了北京师范大学认知神经科学与学习国家重点实验室、IDG/McGovern脑科…

HDU1028——I gnatius and the Princess III

母函数&#xff0c;不解释…… View Code #include <stdio.h>#define N 10001int c1[N], c2[N];int main(){int n,i,j,k;while (scanf("%d", &n) ! EOF ) {for (i0;i<n;i) { c1[i]0; c2[i]0; }for (i0;i<n;…

菜鸟教程 之 JavaScript 函数(function)

From&#xff1a;https://www.runoob.com/js/js-function-definition.html JavaScript 函数定义 JavaScript 使用关键字 function 定义函数。 函数 可以 通过声明定义&#xff0c;函数 也可以 是一个表达式。函数声明 分号 是用来分隔可执行 JavaScript 语句。 由于函数声明不…

深度研报:六大视角解读人工智能,AI岂止于技术

来源&#xff1a; 亿欧摘要&#xff1a; 人工智能只是一项新技术吗&#xff1f;从任一角度诠释AI&#xff0c;都是狭隘并且不完整的。亿欧智库在最新发布的研究报告中&#xff0c;总结两年来的研究成果&#xff0c;从学界、企业、投资、国家等六大视角对人工智能进行了全面解读…

为什么有的机器学习应用公司必将失败?

作者 | Cassie Kozyrkov编译 | Leo出品 | 人工智能头条摘要&#xff1a;告诉大家一个秘密&#xff1a;当人们说起“ 机器学习 ”时&#xff0c;听起来好像只是在谈论一门学科&#xff0c;但其实是两门。如果企业不了解其中的差异&#xff0c;那么就可能招惹来满世界的麻烦。两个…

[转]迭代、集合、字典表和列表

集合在编程的过程中用的是非常的多&#xff0c;如GridViewRowCollection、ConnectionStringSettingsCollection、NameValueCollection等等。一般来说&#xff0c;集合的类都包含在System.Collections命名空间中。那众多集合之间又有什么样的联系呢&#xff1f;这需要我们从集合…

菜鸟教程 之 HTML DOM 和 浏览器BOM

HTML DOM&#xff1a;https://www.runoob.com/js/js-htmldom.html 浏览器对象模型 (BOM)&#xff1a;https://www.runoob.com/js/js-window.html DOM、DOCUMENT、BOM、WINDOW 有什么区别? &#xff1a;https://www.zhihu.com/question/33453164 通过 HTML DOM&#xff0c;Ja…

IBM:物联网应重启,否则注定会失望

来源&#xff1a;IBM商业价值研究院摘要&#xff1a;尽管技术推动着物联网向前发展&#xff0c;但与此同时&#xff0c;缺少有吸引力且可持续盈利的商业模式将阻碍物联网的发展。如果未来的商业模式不同于目前硬件和软件平台的商业模式&#xff0c;那么&#xff0c;它是什么样的…

联通、华为《5G室内覆盖》白皮书!

来源&#xff1a;5G摘要&#xff1a;近日&#xff0c;中国联通、华为联合发布了《面向5G的室内覆盖数字化演进白皮书》。干货报告未来智能实验室是人工智能学家与科学院相关机构联合成立的人工智能&#xff0c;互联网和脑科学交叉研究机构。未来智能实验室的主要工作包括&#…

JavaScript 执行机制

前端开发&#xff0c;一篇文章让你彻底搞懂&#xff0c;什么是JavaScript执行机制&#xff01;&#xff1a;https://zhuanlan.zhihu.com/p/139261821 大白话讲解 JavaScript 执行机制&#xff0c;一看就懂&#xff1a;https://www.jianshu.com/p/22641c97e351 JavaScript 运行…

互联网大脑进化简史,华为云EI智能体加入-2018年7月新版

要&#xff1a;华为云EI智能体是2018年以来产生的第八个类脑智能巨系统&#xff0c;在中国&#xff0c;目前除了小米、联想、今日头条&#xff0c;几乎所有的互联网巨头都提出了自己的”大脑“系统建设计划。1969年互联网诞生以来&#xff0c;网状模型一直是互联网最基础和重要…

Windows 必备神器 Cmder 使用教程

From&#xff1a;终端利器Cmder&#xff1a;https://www.e-learn.cn/topic/3885768 1. 安装 Cmder 打开 Cmder官网&#xff08; https://cmder.net &#xff09;&#xff0c;下拉页面找到 Download 项选择下载&#xff0c;下载的时候&#xff0c;两个版本&#xff0c;分别是 mi…

T-SQL查询进阶--流程控制语句

概述 和其他高级语言一样&#xff0c;T-SQL中也有用于控制流程的语句。T-SQL中的流程控制语句进一步扩展了T-SQL的力量……使得大部分业务逻辑可以在数据库层面进行。但很多人对T-SQL中的流程控制语句并没有系统的了解&#xff0c;本篇文章会系统的对T-SQL语句中的流程控制语句…

潘建伟团队首次实现18个光量子比特纠缠,刷新世界记录

来源&#xff1a;澎湃网摘要&#xff1a;中国科学技术大学潘建伟教授及其同事陆朝阳、刘乃乐、汪喜林等通过调控六个光子的偏振、路径和轨道角动量三个自由度&#xff0c;在国际上首次实现18个光量子比特的纠缠&#xff0c;刷新了所有物理体系中最大纠缠态制备的世界纪录。中国…

Frida Hook 常用函数、java 层 hook、so 层 hook、RPC、群控

From&#xff1a;Frida hook 常用函数分享&#xff1a;https://www.52pojie.cn/thread-1196917-1-1.html From&#xff1a;Frida Hook Android 常用方法&#xff1a;https://blog.csdn.net/zhy025907/article/details/89512096 Frida 使用&#xff1a;https://zhuanlan.zhihu.c…

资本|五大科技巨头并购投资布局分析

来源&#xff1a;199IT互联网数据中心就买进而言&#xff0c;科技巨头可能是一个理想的收购方。Alphabet、亚马逊、苹果、Facebook和微软拥有巨大的市场价值&#xff08;3.9万亿美元&#xff09;。综合来看&#xff0c;五大科技巨头在整个并购市场占有相对较小的份额。下图显示…

安装 Chrome 插件:Stylish、xStyle​、Tampermonkey、SwitchyOmega

安装 Chrome 插件 3 种方法 方法 1&#xff1a;直接通过 chrome 插件商店安装&#xff0c;google 插件商店因为 "都懂的" 原因无法访问&#xff0c;如果会 "高科技" 上网可以忽略。直接通过插件商店安装方法 2&#xff1a;如果不会 "高科技" 上网…

数据库管理工具 Navicat 和 DBeaver

Navicat “Navicat” 是一套可创建多个连接的数据库管理工具&#xff0c;用以方便管理 MySQL、Oracle、PostgreSQL、SQLite、SQL Server、MariaDB 和/或 MongoDB 等不同类型的数据库&#xff0c;并支持管理某些云数据库&#xff0c;例如阿里云、‎腾讯云。Navicat 和 Navicat …

百度Apollo发布中国首个自动驾驶安全报告,L3级别产品2020年量产上市

来源&#xff1a;雷锋网去年 10 月&#xff0c;全球自动驾驶研发先行者 Waymo 发布了长达 43 页的安全报告&#xff0c;里面详细说明了如何装备和训练自动驾驶车辆&#xff0c;从而避免驾驶过程中一些意外情况的发生。今年 1 月&#xff0c;美国车企巨头通用也发布了“2018 自动…