您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > C#-微软官方OleDbHelper
//===============================================================================//OleDbHelperbasedonMicrosoftDataAccessApplicationBlock(DAAB)for.NET//==============================================================================usingSystem;usingSystem.Data;usingSystem.Xml;usingSystem.Data.OleDb;usingSystem.Collections;usingSystem.Configuration;namespaceCommon{///summary///TheOleDbHelperclassisintendedtoencapsulatehighperformance,scalablebestpracticesfor///commonusesofOleDbClient.////summarypublicsealedclassOleDbHelper{#region私有构造函数和方法//Sincethisclassprovidesonlystaticmethods,makethedefaultconstructorprivatetoprevent//instancesfrombeingcreatedwithnewOleDbHelper().privateOleDbHelper(){}///summary///将SqlParameter参数数组(参数值)分配给SqlCommand命令.///这个方法将给任何一个参数分配DBNull.Value;///该操作将阻止默认值的使用.////summary///paramname=command命令名/param///paramname=commandParametersSqlParameters数组/paramprivatestaticvoidAttachParameters(OleDbCommandcommand,OleDbParameter[]commandParameters){if(command==null)thrownewArgumentNullException(command);if(commandParameters!=null){foreach(OleDbParameterpincommandParameters){//checkforderivedoutputvaluewithnovalueassignedif((p.Direction==ParameterDirection.InputOutput)&&(p.Value==null)){p.Value=DBNull.Value;}command.Parameters.Add(p);}}}///summary///将DataRow类型的列值分配到SqlParameter参数数组.////summary///paramname=commandParameters要分配值的SqlParameter参数数组/param///paramname=dataRow将要分配给存储过程参数的DataRow/paramprivatestaticvoidAssignParameterValues(OleDbParameter[]commandParameters,DataRowdataRow){if((commandParameters==null)||(dataRow==null)){return;}inti=0;//设置参数值foreach(OleDbParametercommandParameterincommandParameters){//创建参数名称,如果不存在,只抛出一个异常.if(commandParameter.ParameterName==null||commandParameter.ParameterName.Length=1)thrownewException(string.Format(请提供参数{0}一个有效的名称{1}.,i,commandParameter.ParameterName));//从dataRow的表中获取为参数数组中数组名称的列的索引.//如果存在和参数名称相同的列,则将列值赋给当前名称的参数.if(dataRow.Table.Columns.IndexOf(commandParameter.ParameterName.Substring(1))!=-1)commandParameter.Value=dataRow[commandParameter.ParameterName.Substring(1)];i++;}}///summary///将一个对象数组分配给SqlParameter参数数组.////summary///paramname=commandParameters要分配值的SqlParameter参数数组/param///paramname=parameterValues将要分配给存储过程参数的对象数组/paramprivatestaticvoidAssignParameterValues(OleDbParameter[]commandParameters,object[]parameterValues){if((commandParameters==null)||(parameterValues==null)){return;}//确保对象数组个数与参数个数匹配,如果不匹配,抛出一个异常.if(commandParameters.Length!=parameterValues.Length){thrownewArgumentException(参数值个数与参数不匹配.);}//给参数赋值for(inti=0,j=commandParameters.Length;ij;i++){//IfthecurrentarrayvaluederivesfromIDbDataParameter,thenassignitsValuepropertyif(parameterValues[i]isIDbDataParameter){IDbDataParameterparamInstance=(IDbDataParameter)parameterValues[i];if(paramInstance.Value==null){commandParameters[i].Value=DBNull.Value;}else{commandParameters[i].Value=paramInstance.Value;}}elseif(parameterValues[i]==null){commandParameters[i].Value=DBNull.Value;}else{commandParameters[i].Value=parameterValues[i];}}}///summary///预处理用户提供的命令,数据库连接/事务/命令类型/参数////summary///paramname=command要处理的SqlCommand/param///paramname=connection数据库连接/param///paramname=transaction一个有效的事务或者是null值/param///paramname=commandType命令类型(存储过程,命令文本,其它.)/param///paramname=commandText存储过程名或都T-SQL命令文本/param///paramname=commandParameters和命令相关联的SqlParameter参数数组,如果没有参数为'null'/param///paramname=mustCloseConnectionctrue/c如果连接是打开的,则为true,其它情况下为false./paramprivatestaticvoidPrepareCommand(OleDbCommandcommand,OleDbConnectionconnection,OleDbTransactiontransaction,CommandTypecommandType,stringcommandText,OleDbParameter[]commandParameters,outboolmustCloseConnection){if(command==null)thrownewArgumentNullException(command);if(commandText==null||commandText.Length==0)thrownewArgumentNullException(commandText);//Iftheprovidedconnectionisnotopen,wewillopenitif(connection.State!=ConnectionState.Open){mustCloseConnection=true;connection.Open();}else{mustCloseConnection=false;}//给命令分配一个数据库连接.command.Connection=connection;//设置命令文本(存储过程名或SQL语句)command.CommandText=commandText;//分配事务if(transaction!=null){if(transaction.Connection==null)thrownewArgumentException(Thetransactionwasrollbackedorcommited,pleaseprovideanopentransaction.,transaction);command.Transaction=transaction;}//设置命令类型.command.CommandType=commandType;//分配命令参数if(commandParameters!=null){AttachParameters(command,commandParameters);}return;}#endreg
本文标题:C#-微软官方OleDbHelper
链接地址:https://www.777doc.com/doc-5327623 .html