Java是如何快速煮成C 的 (二) 数据访问 1

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

在上两篇《 Java是如何快速煮成C#的?》(一):相似的方法 《Java是如何快速煮成C#的?》(一):相似的方法(2) 中,我们开启了C#与Java中的话题之一:相似的方法。其实我写主这两篇文章的目的,有两个:
1、总结自己的学习历程,这个是主要的。
2、给c#转入java的同学一个快速熟悉的感觉,所以这个系列的名称也是“快速煮成”。
因为我对这两门语言仅限了解,对c#也仅限于熟悉,如有理解不妥之处,请指正。
今天我们看看这两种语言环境下纯粹的数据访问。
首先我再次声明:
1、本文不比较这两种语言的数据访问的性能差异。
2、本文不涉及各自的OR框架, 如C#的ADO.NET Entity Framework,MVC,Nhibernate,spring.net,以及Java领域的Spring/Hibernate/Struts等第三方框架,只是纯粹的数据访问
3、数据库采用MS SQL server 2008,其实也可以用mySQL,MySQL提供官方支持。oracle平时很少用,DB2没用过。
准备工作:一个用于测试的部门表DepartDemo,表结构如下:
 邀月工作室
相关的SQL语句:

  1. Create database Db2010Demo  
  2. go  
  3. use Db2010Demo  
  4. go  
  5. if exists (select 1  
  6. from sysobjects  
  7. where id = object_id('DepartDemo')  
  8. and type = 'U')  
  9. drop table DepartDemo  
  10. go  
  11. /*==============================================================*/  
  12. /* Table: DepartDemo */  
  13. /*==============================================================*/  
  14. create table DepartDemo (  
  15. PKID int identity(101,1),  
  16. DName nvarchar(200) null,  
  17. DCode nvarchar(500) null,  
  18. Manager nvarchar(50) null,  
  19. ParentID int null default 0,  
  20. AddUser nvarchar(50) null,  
  21. AddTime datetime null,  
  22. ModUser nvarchar(50) null,  
  23. ModTime datetime null,  
  24. CurState smallint not null default 0,  
  25. Remark nvarchar(500) null,  
  26. F1 int not null default 0,  
  27. F2 nvarchar(300) null,  
  28. constraint PK_DEPARTDEMO primary key (PKID)  
  29. )  
  30. go  
  31. --插入一条测试数据  
  32. insert into DepartDemo  
  33. select '国家统计局房产审计一科','0','胡不归',0,'DeomUser',getdate(),  
  34. '','1900-01-01',1,'专业评估全国房价,为老百姓谋福祉',0,''  
  35. --创建一个存储过程,在C#程序中用到  
  36. IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[DepartDemoSelectByPKID]') AND type='P')  
  37. DROP PROCEDURE [dbo].[DepartDemoSelectByPKID]  
  38. GO  
  39. CREATE PROCEDURE [dbo].[DepartDemoSelectByPKID]  
  40. (  
  41. @Pkid int  
  42. )  
  43. AS  
  44. BEGIN TRAN  
  45. Select * From [DepartDemo] Where [Pkid]=@Pkid  
  46. IF @@ERROR!=0  
  47. BEGIN  
  48. ROLLBACK  
  49. END  
  50. ELSE  
  51. BEGIN  
  52. COMMIT  
  53. END  
  54. GO  
Create database Db2010Demogouse Db2010Demogoif exists (select 1from sysobjectswhere id = object_id('DepartDemo')and type = 'U')drop table DepartDemogo/*==============================================================*//* Table: DepartDemo *//*==============================================================*/create table DepartDemo (PKID int identity(101,1),DName nvarchar(200) null,DCode nvarchar(500) null,Manager nvarchar(50) null,ParentID int null default 0,AddUser nvarchar(50) null,AddTime datetime null,ModUser nvarchar(50) null,ModTime datetime null,CurState smallint not null default 0,Remark nvarchar(500) null,F1 int not null default 0,F2 nvarchar(300) null,constraint PK_DEPARTDEMO primary key (PKID))go--插入一条测试数据insert into DepartDemoselect '国家统计局房产审计一科','0','胡不归',0,'DeomUser',getdate(),'','1900-01-01',1,'专业评估全国房价,为老百姓谋福祉',0,''--创建一个存储过程,在C#程序中用到IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[DepartDemoSelectByPKID]') AND type='P')DROP PROCEDURE [dbo].[DepartDemoSelectByPKID]GOCREATE PROCEDURE [dbo].[DepartDemoSelectByPKID](@Pkid int)ASBEGIN TRANSelect * From [DepartDemo] Where [Pkid]=@PkidIF @@ERROR!=0BEGINROLLBACKENDELSEBEGINCOMMITENDGO

一、我们看看C#环境下一个数据访问的简单例子。

在vs2010中新建一控制台项目,结构如下:

邀月工作室

相应的代码,

基础数据层:

Database.cs:

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. namespace demo2010.database  
  7. {  
  8.     public class DataBase  
  9.     {  
  10.         private static string connectionString = System.Configuration.ConfigurationManager.AppSettings["GlobalsConnString"];  
  11.         public static string ConnectionString  
  12.         {  
  13.             get { return connectionString; }  
  14.             set { connectionString = value; }  
  15.         }  
  16.         #region Helpers  
  17.         internal protected static IDataReader GetReader(string commandText, SqlParameter[] p)  
  18.         {  
  19.             return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, commandText, p);  
  20.         }  
  21.         internal protected static IDataReader GetReader(string commandText)  
  22.         {  
  23.             return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, commandText);  
  24.         }  
  25.         internal protected static int NonQueryInt(string commandText, SqlParameter[] p)  
  26.         {  
  27.             return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, commandText, p);  
  28.         }  
  29.         internal protected static bool NonQueryBool(string commandText, SqlParameter[] p)  
  30.         {  
  31.             return NonQueryInt(commandText, p) > 0;  
  32.         }  
  33.         internal protected void RunSql(string commandText)  
  34.         {  
  35.             SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, commandText);  
  36.         }  
  37.         public static void ExecuteSQL(string commandText)  
  38.         {  
  39.             SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, commandText);  
  40.         }  
  41.         public static DataTable GetDataTable(string commandText)  
  42.         {  
  43.             return SqlHelper.ExecuteDataTable(ConnectionString, CommandType.Text, commandText);  
  44.         }  
  45.         public static DataTable GetDataTable(string commandText, CommandType commandType)  
  46.         {  
  47.             return SqlHelper.ExecuteDataTable(ConnectionString, commandType, commandText);  
  48.         }  
  49.          
  50.         internal protected static string ReturnString(string commandText, SqlParameter[] p)  
  51.         {  
  52.             object result = SqlHelper.ExecuteScalar(ConnectionString, System.Data.CommandType.StoredProcedure, commandText, p);  
  53.             return result.ToString();  
  54.         }  
  55.         internal protected static SqlParameter ReTurnStringValue  
  56.         {  
  57.             get  
  58.             {  
  59.                 return SqlHelper.MakeOutParam("@ReTurnValue", SqlDbType.NVarChar, 200);  
  60.             }  
  61.         }  
  62.         internal protected static SqlParameter ReTurnLongValue  
  63.         {  
  64.             get  
  65.             {  
  66.                 return SqlHelper.MakeOutParam("@ReTurnValue", SqlDbType.BigInt, 8);  
  67.             }  
  68.         }  
  69.         #endregion  
  70.     }  
  71. }  
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;namespace demo2010.database{    public class DataBase    {        private static string connectionString = System.Configuration.ConfigurationManager.AppSettings["GlobalsConnString"];        public static string ConnectionString        {            get { return connectionString; }            set { connectionString = value; }        }        #region Helpers        internal protected static IDataReader GetReader(string commandText, SqlParameter[] p)        {            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, commandText, p);        }        internal protected static IDataReader GetReader(string commandText)        {            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, commandText);        }        internal protected static int NonQueryInt(string commandText, SqlParameter[] p)        {            return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, commandText, p);        }        internal protected static bool NonQueryBool(string commandText, SqlParameter[] p)        {            return NonQueryInt(commandText, p) > 0;        }        internal protected void RunSql(string commandText)        {            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, commandText);        }        public static void ExecuteSQL(string commandText)        {            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, commandText);        }        public static DataTable GetDataTable(string commandText)        {            return SqlHelper.ExecuteDataTable(ConnectionString, CommandType.Text, commandText);        }        public static DataTable GetDataTable(string commandText, CommandType commandType)        {            return SqlHelper.ExecuteDataTable(ConnectionString, commandType, commandText);        }               internal protected static string ReturnString(string commandText, SqlParameter[] p)        {            object result = SqlHelper.ExecuteScalar(ConnectionString, System.Data.CommandType.StoredProcedure, commandText, p);            return result.ToString();        }        internal protected static SqlParameter ReTurnStringValue        {            get            {                return SqlHelper.MakeOutParam("@ReTurnValue", SqlDbType.NVarChar, 200);            }        }        internal protected static SqlParameter ReTurnLongValue        {            get            {                return SqlHelper.MakeOutParam("@ReTurnValue", SqlDbType.BigInt, 8);            }        }        #endregion    }}

DynamicBuilder.cs:

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data.SqlClient;  
  4. using System.Reflection;  
  5. using System.Reflection.Emit;  
  6. using System.Data;  
  7. namespace demo2010.database  
  8. {  
  9.     /// <summary>  
  10.     /// Tony 2008.8.28 Add   
  11.     /// </summary>  
  12.     /// <typeparam name="T"></typeparam>  
  13.     public class DynamicBuilder<T>  
  14.     {  
  15.         private static readonly MethodInfo getValueMethod = typeof(IDataRecord).GetMethod("get_Item"new Type[] { typeof(int) });  
  16.         private static readonly MethodInfo isDBNullMethod = typeof(IDataRecord).GetMethod("IsDBNull"new Type[] { typeof(int) });  
  17.         private delegate T Load(IDataRecord dataRecord);  
  18.         private const BindingFlags BINDING_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase;//Added by Tony 2008.09.25  
  19.         private Load handler;  
  20.         private DynamicBuilder() { }  
  21.         public T Build(IDataRecord dataRecord)  
  22.         {  
  23.             return handler(dataRecord);  
  24.         }  
  25.         public static DynamicBuilder<T> CreateBuilder(IDataRecord dataRecord)  
  26.         {  
  27.             DynamicBuilder<T> dynamicBuilder = new DynamicBuilder<T>();  
  28.             DynamicMethod method = new DynamicMethod("DynamicCreate"typeof(T), new Type[] { typeof(IDataRecord) }, typeof(T), true);  
  29.             ILGenerator generator = method.GetILGenerator();  
  30.             LocalBuilder result = generator.DeclareLocal(typeof(T));  
  31.             generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));  
  32.             generator.Emit(OpCodes.Stloc, result);  
  33.             for (int i = 0; i < dataRecord.FieldCount; i++)  
  34.             {  
  35.                 PropertyInfo propertyInfo = typeof(T).GetProperty(dataRecord.GetName(i),BINDING_FLAGS);//performance'Maxvalue decrease 5% **** Test By Tony 2008.09.25   
  36.                 Label endIfLabel = generator.DefineLabel();  
  37.                 if (propertyInfo != null && propertyInfo.GetSetMethod() != null)  
  38.                 {  
  39.                     generator.Emit(OpCodes.Ldarg_0);  
  40.                     generator.Emit(OpCodes.Ldc_I4, i);  
  41.                     generator.Emit(OpCodes.Callvirt, isDBNullMethod);  
  42.                     generator.Emit(OpCodes.Brtrue, endIfLabel);  
  43.                     generator.Emit(OpCodes.Ldloc, result);  
  44.                     generator.Emit(OpCodes.Ldarg_0);  
  45.                     generator.Emit(OpCodes.Ldc_I4, i);  
  46.                     generator.Emit(OpCodes.Callvirt, getValueMethod);  
  47.                     generator.Emit(OpCodes.Unbox_Any, dataRecord.GetFieldType(i));  
  48.                     generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());  
  49.                     generator.MarkLabel(endIfLabel);  
  50.                 }  
  51.             }  
  52.             generator.Emit(OpCodes.Ldloc, result);  
  53.             generator.Emit(OpCodes.Ret);  
  54.             dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load));  
  55.             return dynamicBuilder;  
  56.         }  
  57.     }  
  58. }  
using System;using System.Collections.Generic;using System.Data.SqlClient;using System.Reflection;using System.Reflection.Emit;using System.Data;namespace demo2010.database{    /// <summary>    /// Tony 2008.8.28 Add     /// </summary>    /// <typeparam name="T"></typeparam>    public class DynamicBuilder<T>    {        private static readonly MethodInfo getValueMethod = typeof(IDataRecord).GetMethod("get_Item", new Type[] { typeof(int) });        private static readonly MethodInfo isDBNullMethod = typeof(IDataRecord).GetMethod("IsDBNull", new Type[] { typeof(int) });        private delegate T Load(IDataRecord dataRecord);        private const BindingFlags BINDING_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase;//Added by Tony 2008.09.25        private Load handler;        private DynamicBuilder() { }        public T Build(IDataRecord dataRecord)        {            return handler(dataRecord);        }        public static DynamicBuilder<T> CreateBuilder(IDataRecord dataRecord)        {            DynamicBuilder<T> dynamicBuilder = new DynamicBuilder<T>();            DynamicMethod method = new DynamicMethod("DynamicCreate", typeof(T), new Type[] { typeof(IDataRecord) }, typeof(T), true);            ILGenerator generator = method.GetILGenerator();            LocalBuilder result = generator.DeclareLocal(typeof(T));            generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));            generator.Emit(OpCodes.Stloc, result);            for (int i = 0; i < dataRecord.FieldCount; i++)            {                PropertyInfo propertyInfo = typeof(T).GetProperty(dataRecord.GetName(i),BINDING_FLAGS);//performance'Maxvalue decrease 5% **** Test By Tony 2008.09.25                 Label endIfLabel = generator.DefineLabel();                if (propertyInfo != null && propertyInfo.GetSetMethod() != null)                {                    generator.Emit(OpCodes.Ldarg_0);                    generator.Emit(OpCodes.Ldc_I4, i);                    generator.Emit(OpCodes.Callvirt, isDBNullMethod);                    generator.Emit(OpCodes.Brtrue, endIfLabel);                    generator.Emit(OpCodes.Ldloc, result);                    generator.Emit(OpCodes.Ldarg_0);                    generator.Emit(OpCodes.Ldc_I4, i);                    generator.Emit(OpCodes.Callvirt, getValueMethod);                    generator.Emit(OpCodes.Unbox_Any, dataRecord.GetFieldType(i));                    generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());                    generator.MarkLabel(endIfLabel);                }            }            generator.Emit(OpCodes.Ldloc, result);            generator.Emit(OpCodes.Ret);            dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load));            return dynamicBuilder;        }    }}

SqlHelper.cs:(我们将最常用的SQL在大家熟悉的SqlHelper中。)

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Xml;  
  5. using System.Data.SqlClient;  
  6. using System.Collections;  
  7. namespace demo2010.database  
  8. {  
  9.     #region Disclaimer/Info  
  10.     #endregion  
  11.     /// <summary>  
  12.     /// The SqlHelper class is intended to encapsulate high performance, scalable best practices for   
  13.     /// common uses of SqlClient.  
  14.     /// </summary>  
  15.     public sealed class SqlHelper  
  16.     {  
  17.         #region private utility methods & constructors  
  18.         //Since this class provides only static methods, make the default constructor private to prevent   
  19.         //instances from being created with "new SqlHelper()".  
  20.         private SqlHelper() { }  
  21.         /// <summary>  
  22.         /// This method is used to attach array of SqlParameters to a SqlCommand.  
  23.         ///   
  24.         /// This method will assign a value of DbNull to any parameter with a direction of  
  25.         /// InputOutput and a value of null.    
  26.         ///   
  27.         /// This behavior will prevent default values from being used, but  
  28.         /// this will be the less common case than an intended pure output parameter (derived as InputOutput)  
  29.         /// where the user provided no input value.  
  30.         /// </summary>  
  31.         /// <param name="command">The command to which the parameters will be added</param>  
  32.         /// <param name="commandParameters">an array of SqlParameters tho be added to command</param>  
  33.         private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)  
  34.         {  
  35.             foreach (SqlParameter p in commandParameters)  
  36.             {  
  37.                 //check for derived output value with no value assigned  
  38.                 if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))  
  39.                 {  
  40.                     p.Value = DBNull.Value;  
  41.                 }  
  42.                 command.Parameters.Add(p);  
  43.             }  
  44.         }  
  45.         /// <summary>  
  46.         /// This method assigns an array of values to an array of SqlParameters.  
  47.         /// </summary>  
  48.         /// <param name="commandParameters">array of SqlParameters to be assigned values</param>  
  49.         /// <param name="parameterValues">array of Components holding the values to be assigned</param>  
  50.         private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)  
  51.         {  
  52.             if ((commandParameters == null) || (parameterValues == null))  
  53.             {  
  54.                 //do nothing if we get no data  
  55.                 return;  
  56.             }  
  57.             // we must have the same number of values as we pave parameters to put them in  
  58.             if (commandParameters.Length != parameterValues.Length)  
  59.             {  
  60.                 throw new ArgumentException("Parameter count does not match Parameter Value count.");  
  61.             }  
  62.             //iterate through the SqlParameters, assigning the values from the corresponding position in the   
  63.             //value array  
  64.             for (int i = 0, j = commandParameters.Length; i < j; i++)  
  65.             {  
  66.                 commandParameters[i].Value = parameterValues[i];  
  67.             }  
  68.         }  
  69.         /// <summary>  
  70.         /// This method opens (if necessary) and assigns a connection, transaction, command type and parameters   
  71.         /// to the provided command.  
  72.         /// </summary>  
  73.         /// <param name="command">the SqlCommand to be prepared</param>  
  74.         /// <param name="connection">a valid SqlConnection, on which to execute this command</param>  
  75.         /// <param name="transaction">a valid SqlTransaction, or 'null'</param>  
  76.         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>  
  77.         /// <param name="commandText">the stored procedure name or T-SQL command</param>  
  78.         /// <param name="commandParameters">an array of SqlParameters to be associated with the command or 'null' if no parameters are required</param>  
  79.         private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)  
  80.         {  
  81.             //if the provided connection is not open, we will open it  
  82.             if (connection.State != ConnectionState.Open)  
  83.             {  
  84.                 connection.Open();  
  85.             }  
  86.             //associate the connection with the command  
  87.             command.Connection = connection;  
  88.             //set the command text (stored procedure name or SQL statement)  
  89.             command.CommandText = commandText;  
  90.             //if we were provided a transaction, assign it.  
  91.             if (transaction != null)  
  92.             {  
  93.                 command.Transaction = transaction;  
  94.             }  
  95.             //set the command type  
  96.             command.CommandType = commandType;  
  97.             //attach the command parameters if they are provided  
  98.             if (commandParameters != null)  
  99.             {  
  100.                 AttachParameters(command, commandParameters);  
  101.             }  
  102.             return;  
  103.         }  
  104.         #endregion private utility methods & constructors  
  105.         #region DataHelpers  
  106.         public static string CheckNull(object obj)  
  107.         {  
  108.             return (string)obj;  
  109.         }  
  110.         public static string CheckNull(DBNull obj)  
  111.         {  
  112.             return null;  
  113.         }  
  114.         #endregion  
  115.         #region AddParameters  
  116.         public static object CheckForNullString(string text)  
  117.         {  
  118.             if (text == null || text.Trim().Length == 0)  
  119.             {  
  120.                 return string.Empty;  
  121.                 //return System.DBNull.Value;  
  122.             }  
  123.             else  
  124.             {  
  125.                 return text;  
  126.             }  
  127.         }  
  128.         public static object CheckForNullDateTime(object obj)  
  129.         {  
  130.             if (obj == null)  
  131.             {  
  132.                 return Convert.ToDateTime("1900-01-01");  
  133.                 //return System.DBNull.Value;  
  134.             }  
  135.             else  
  136.             {  
  137.                 return obj;  
  138.             }  
  139.         }  
  140.         public static SqlParameter MakeInParam(string ParamName, object Value)  
  141.         {  
  142.             return new SqlParameter(ParamName, Value);  
  143.         }  
  144.         /// <summary>  
  145.         /// Make input param.  
  146.         /// </summary>  
  147.         /// <param name="ParamName">Name of param.</param>  
  148.         /// <param name="DbType">Param type.</param>  
  149.         /// <param name="Size">Param size.</param>  
  150.         /// <param name="Value">Param value.</param>  
  151.         /// <returns>New parameter.</returns>  
  152.         public static SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)  
  153.         {  
  154.             return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);  
  155.         }  
  156.         /// <summary>  
  157.         /// Make input param.  
  158.         /// </summary>  
  159.         /// <param name="ParamName">Name of param.</param>  
  160.         /// <param name="DbType">Param type.</param>  
  161.         /// <param name="Size">Param size.</param>  
  162.         /// <returns>New parameter.</returns>  
  163.         public static SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)  
  164.         {  
  165.             return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);  
  166.         }  
  167.         /// <summary>  
  168.         /// Make stored procedure param.  
  169.         /// </summary>  
  170.         /// <param name="ParamName">Name of param.</param>  
  171.         /// <param name="DbType">Param type.</param>  
  172.         /// <param name="Size">Param size.</param>  
  173.         /// <param name="Direction">Parm direction.</param>  
  174.         /// <param name="Value">Param value.</param>  
  175.         /// <returns>New parameter.</returns>  
  176.         public static SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)  
  177.         {  
  178.             SqlParameter param;  
  179.             if (Size > 0)  
  180.                 param = new SqlParameter(ParamName, DbType, Size);  
  181.             else  
  182.                 param = new SqlParameter(ParamName, DbType);  
  183.             param.Direction = Direction;  
  184.             if (!(Direction == ParameterDirection.Output && Value == null))  
  185.                 param.Value = Value;  
  186.             return param;  
  187.         }  
  188.         #endregion  
  189.         #region ExecuteNonQuery  
  190.         /// <summary>  
  191.         /// Execute a SqlCommand (that returns no resultset and takes no parameters) against the database specified in   
  192.         /// the connection string.   
  193.         /// </summary>  
  194.         /// <remarks>  
  195.         /// e.g.:    
  196.         ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders");  
  197.         /// </remarks>  
  198.         /// <param name="connectionString">a valid connection string for a SqlConnection</param>  
  199.         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>  
  200.         /// <param name="commandText">the stored procedure name or T-SQL command</param>  
  201.         /// <returns>an int representing the number of rows affected by the command</returns>  
  202.         public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)  
  203.         {  
  204.             //pass through the call providing null for the set of SqlParameters  
  205.             return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null);  
  206.         }  
  207.         /// <summary>  
  208.         /// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string   
  209.         /// using the provided parameters.  
  210.         /// </summary>  
  211.         /// <remarks>  
  212.         /// e.g.:    
  213.         ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));  
  214.         /// </remarks>  
  215.         /// <param name="connectionString">a valid connection string for a SqlConnection</param>  
  216.         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>  
  217.         /// <param name="commandText">the stored procedure name or T-SQL command</param>  
  218.         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>  
  219.         /// <returns>an int representing the number of rows affected by the command</returns>  
  220.         public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)  
  221.         {  
  222.             //create & open a SqlConnection, and dispose of it after we are done.  
  223.             using (SqlConnection cn = new SqlConnection(connectionString))  
  224.             {  
  225.                 cn.Open();  
  226.                 //call the overload that takes a connection in place of the connection string  
  227.                 return ExecuteNonQuery(cn, commandType, commandText, commandParameters);  
  228.             }  
  229.         }  
  230.         /// <summary>  
  231.         /// Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlConnection.   
  232.         /// </summary>  
  233.         /// <remarks>  
  234.         /// e.g.:    
  235.         ///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders");  
  236.         /// </remarks>  
  237.         /// <param name="connection">a valid SqlConnection</param>  
  238.         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>  
  239.         /// <param name="commandText">the stored procedure name or T-SQL command</param>  
  240.         /// <returns>an int representing the number of rows affected by the command</returns>  
  241.         public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText)  
  242.         {  
  243.             //pass through the call providing null for the set of SqlParameters  
  244.             return ExecuteNonQuery(connection, commandType, commandText, (SqlParameter[])null);  
  245.         }  
  246.         /// <summary>  
  247.         /// Execute a SqlCommand (that returns no resultset) against the specified SqlConnection   
  248.         /// using the provided parameters.  
  249.         /// </summary>  
  250.         /// <remarks>  
  251.         /// e.g.:    
  252.         ///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));  
  253.         /// </remarks>  
  254.         /// <param name="connection">a valid SqlConnection</param>  
  255.         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>  
  256.         /// <param name="commandText">the stored procedure name or T-SQL command</param>  
  257.         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>  
  258.         /// <returns>an int representing the number of rows affected by the command</returns>  
  259.         public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)  
  260.         {  
  261.             //create a command and prepare it for execution  
  262.             SqlCommand cmd = new SqlCommand();  
  263.             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters);  
  264.             //finally, execute the command.  
  265.             int retval = cmd.ExecuteNonQuery();  
  266.             // detach the SqlParameters from the command object, so they can be used again.  
  267.             cmd.Parameters.Clear();  
  268.             return retval;  
  269.         }  
  270.         /// <summary>  
  271.         /// Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlTransaction.   
  272.         /// </summary>  
  273.         /// <remarks>  
  274.         /// e.g.:    
  275.         ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");  
  276.         /// </remarks>  
  277.         /// <param name="transaction">a valid SqlTransaction</param>  
  278.         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>  
  279.         /// <param name="commandText">the stored procedure name or T-SQL command</param>  
  280.         /// <returns>an int representing the number of rows affected by the command</returns>  
  281.         public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText)  
  282.         {  
  283.             //pass through the call providing null for the set of SqlParameters  
  284.             return ExecuteNonQuery(transaction, commandType, commandText, (SqlParameter[])null);  
  285.         }  
  286.         /// <summary>  
  287.         /// Execute a SqlCommand (that returns no resultset) against the specified SqlTransaction  
  288.         /// using the provided parameters.  
  289.         /// </summary>  
  290.         /// <remarks>  
  291.         /// e.g.:    
  292.         ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));  
  293.         /// </remarks>  
  294.         /// <param name="transaction">a valid SqlTransaction</param>  
  295.         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>  
  296.         /// <param name="commandText">the stored procedure name or T-SQL command</param>  
  297.         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>  
  298.         /// <returns>an int representing the number of rows affected by the command</returns>  
  299.         public static int ExecuteNonQuery(SqlTransaction transaction, C

    给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

    这里写图片描述

猜你喜欢

转载自blog.csdn.net/gfuugfdkkn/article/details/84101894