SqlHelper- -高悬的双刃剑(到底好用么~~)

- -本人刚刚从病魔中~~逃生- -差点就归西了^_^..先说说SqlHelper:

//---- SqlHelper- -主角要出场了- -怎么说呢下面的这个我看还不错哈

ContractedBlock.gif ExpandedBlockStart.gif    CreateCommand创建命令 #region CreateCommand创建命令
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建一个由存储过程提供的命令
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  SqlCommand command = CreateCommand(conn, "AddCustomer", "CustomerID", "CustomerName");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">一个合法的连接</param>
InBlock.gif        
/// <param name="spName">存储过程名</param>
InBlock.gif        
/// <param name="sourceColumns">源列名称数组</param>
ExpandedSubBlockEnd.gif        
/// <returns>一个合法的命令</returns>

InBlock.gif        internal static SqlCommand CreateCommand(SqlConnection connection, string spName, params string[] sourceColumns)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (connection == nullthrow new ArgumentException("connection");
InBlock.gif            
if (spName == null || spName.Length == 0throw new ArgumentException("spName");
InBlock.gif
InBlock.gif            
//创建一个命令
InBlock.gif
            SqlCommand cmd = new SqlCommand(spName, connection);
InBlock.gif            cmd.CommandType 
= CommandType.StoredProcedure;
InBlock.gif
InBlock.gif            
//如果接受一个参数,则处理它
InBlock.gif
            if ((sourceColumns != null&& (sourceColumns.Length > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//提取存储过程参数
InBlock.gif
                SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
InBlock.gif
InBlock.gif                
//设置源列的名称
InBlock.gif
                for (int index = 0; index < sourceColumns.Length; index++)
InBlock.gif                    commandParameters[index].SourceColumn 
= sourceColumns[index];
InBlock.gif
InBlock.gif                
//将参数附加到命令上
InBlock.gif
                AttachParameters(cmd, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return cmd;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 这个方法将一个数组的值赋值到一个SqlParameter数组
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="commandParameters">要被赋值的SqlParameter数组</param>
ExpandedBlockEnd.gif        
/// <param name="parameterValues">一个包含参数值的object数组</param>

None.gif          private   static   void  AssignParameterValues(SqlParameter[] commandParameters,  object [] parameterValues)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if ((commandParameters == null|| parameterValues == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//如果没有数据则返回
InBlock.gif
                return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//参数的数量必须与值得数量匹配
InBlock.gif
            if (commandParameters.Length != parameterValues.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ArgumentException("参数的个数不能匹配参数值的个数");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//迭代参数数组,把object数组的值赋给相应的参数
InBlock.gif
            for (int i = 0, j = commandParameters.Length; i < j; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//如果数组的值继承自IDbDataParameter,这是赋给它的属性值
InBlock.gif
                if (parameterValues[i] is IDbDataParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    IDbDataParameter paraInstance 
= (IDbDataParameter)parameterValues[i];
InBlock.gif                    
if (paraInstance.Value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        commandParameters[i].Value 
= DBNull.Value;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        commandParameters[i].Value 
= paraInstance.Value;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (parameterValues[i] == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    commandParameters[i].Value 
= DBNull.Value;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    commandParameters[i].Value 
= parameterValues[i];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }



//--上面的这个也不错,--其他的我也分析清楚了但是感觉一个晕为什么呢~~这个类的重载好多阿,- -不太喜欢故而不写出来- -

SqlHelperParameterCache- -一般就不要用了,浪费内存但是某些特殊情况不如,自动生成数据实体的时候- -瓦塞塞,好用级了,- -可以通过这家伙生成所有存储过程的实体,- -某些程序就是这么干的- -我们先来看看

ExpandedBlockStart.gif ContractedBlock.gif     /**/ /// <summary>
InBlock.gif    
/// SqlHelperParameterCache 提供一些函数用来发现存储过程的参数
ExpandedBlockEnd.gif    
/// </summary>

None.gif      internal   sealed   class  SqlHelperParameterCache
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif        
private methods, variables, and constructors#region private methods, variables, and constructors
InBlock.gif
InBlock.gif        
//这个类仅仅提供静态方法,并使用一个私有的构造器来阻止创建一个实例化对象
ExpandedSubBlockStart.gifContractedSubBlock.gif
        private SqlHelperParameterCache() dot.gif{ }
InBlock.gif
InBlock.gif        
private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 在运行时发现存储过程
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connection">一个合法的连接对象</param>
InBlock.gif        
/// <param name="spName">存储过程名</param>
InBlock.gif        
/// <param name="includeReturnValueParameter">是否包含返回的参数</param>
ExpandedSubBlockEnd.gif        
/// <returns>被发现的参数数组</returns>

InBlock.gif        private static SqlParameter[] DiscoverSpParameterSet(SqlConnection connection, string spName, bool includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (connection == nullthrow new ArgumentNullException("connection");
InBlock.gif            
if (spName == null || spName.Length == 0throw new ArgumentNullException("spName");
InBlock.gif
InBlock.gif            SqlCommand cmd 
= new SqlCommand(spName, connection);
InBlock.gif            cmd.CommandType 
= CommandType.StoredProcedure;
InBlock.gif
InBlock.gif            connection.Open();
InBlock.gif            SqlCommandBuilder.DeriveParameters(cmd);
InBlock.gif            connection.Close();
InBlock.gif
InBlock.gif            
if (!includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cmd.Parameters.RemoveAt(
0);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            SqlParameter[] discoveredParameters 
= new SqlParameter[cmd.Parameters.Count];
InBlock.gif
InBlock.gif            cmd.Parameters.CopyTo(discoveredParameters, 
0);
InBlock.gif
InBlock.gif            
// 将参数置为DBNull.Value
InBlock.gif
            foreach (SqlParameter discoveredParameter in discoveredParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                discoveredParameter.Value 
= DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return discoveredParameters;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 深度拷贝参数数组
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="originalParameters"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private static SqlParameter[] CloneParameters(SqlParameter[] originalParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter[] clonedParameters 
= new SqlParameter[originalParameters.Length];
InBlock.gif
InBlock.gif            
for (int i = 0, j = originalParameters.Length; i < j; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                clonedParameters[i] 
= (SqlParameter)((ICloneable)originalParameters[i]).Clone();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return clonedParameters;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 private methods, variables, and constructors
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
caching functions#region caching functions
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将参数数组添加到缓存
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connectionString">一个合法的连接字符串</param>
InBlock.gif        
/// <param name="commandText">命令文本</param>
ExpandedSubBlockEnd.gif        
/// <param name="commandParameters">被缓存的参数数组</param>

InBlock.gif        internal static void CacheParameterSet(string connectionString, string commandText, params SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (connectionString == null || connectionString.Length == 0throw new ArgumentNullException("connectionString");
InBlock.gif            
if (commandText == null || commandText.Length == 0throw new ArgumentNullException("commandText");
InBlock.gif
InBlock.gif            
string hashKey = connectionString + ":" + commandText;
InBlock.gif
InBlock.gif            paramCache[hashKey] 
= commandParameters;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从缓存中提取参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connectionString">一个合法的连接字符串</param>
InBlock.gif        
/// <param name="commandText">命令文本</param>
ExpandedSubBlockEnd.gif        
/// <returns>参数数组</returns>

InBlock.gif        internal static SqlParameter[] GetCachedParameterSet(string connectionString, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (connectionString == null || connectionString.Length == 0throw new ArgumentNullException("connectionString");
InBlock.gif            
if (commandText == null || commandText.Length == 0throw new ArgumentNullException("commandText");
InBlock.gif
InBlock.gif            
string hashKey = connectionString + ":" + commandText;
InBlock.gif
InBlock.gif            SqlParameter[] cachedParameters 
= paramCache[hashKey] as SqlParameter[];
InBlock.gif            
if (cachedParameters == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//为什么要进行克隆
InBlock.gif
                return CloneParameters(cachedParameters);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 caching functions
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Parameter Discovery Functions#region Parameter Discovery Functions
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取存储过程的参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// 这个方法将查询数据库,并将其放置在缓存中
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">一个合法的连接</param>
InBlock.gif        
/// <param name="spName">存储过程名</param>
ExpandedSubBlockEnd.gif        
/// <returns>数组参数</returns>

InBlock.gif        internal static SqlParameter[] GetSpParameterSet(string connectionString, string spName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetSpParameterSet(connectionString, spName, false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取存储过程的参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// 这个方法将查询数据库,并将其放置在缓存中
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">一个合法的连接</param>
InBlock.gif        
/// <param name="spName">存储过程名</param>
InBlock.gif        
/// <param name="includeReturnValueParameter">是否在结果中返回参数值</param>
ExpandedSubBlockEnd.gif        
/// <returns>参数数组</returns>

InBlock.gif        internal static SqlParameter[] GetSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (connectionString == null || connectionString.Length == 0throw new ArgumentNullException("connectionString");
InBlock.gif            
if (spName == null || spName.Length == 0throw new ArgumentNullException("spName");
InBlock.gif
InBlock.gif            
using (SqlConnection connection = new SqlConnection(connectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return GetSpParameterSetInternal(connection, spName, includeReturnValueParameter);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取存储过程的参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// 这个方法将查询数据库,并将其放置在缓存中
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">一个合法的连接对象</param>
InBlock.gif        
/// <param name="spName">存储过程名</param>
ExpandedSubBlockEnd.gif        
/// <returns>参数数组</returns>

InBlock.gif        internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetSpParameterSet(connection, spName, false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///  获取存储过程的参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// 这个方法将查询数据库,并将其放置在缓存中
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">一个合法的连接对象</param>
InBlock.gif        
/// <param name="spName">存储过程名</param>
InBlock.gif        
/// <param name="includeReturnValueParameter">是否在结果中返回参数值</param>
ExpandedSubBlockEnd.gif        
/// <returns>参数数组</returns>

InBlock.gif        internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName, bool includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (connection == nullthrow new ArgumentNullException("connection");
InBlock.gif            
using (SqlConnection clonedConnection = (SqlConnection)((ICloneable)connection).Clone())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return GetSpParameterSetInternal(clonedConnection, spName, includeReturnValueParameter);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 这个方法将查询数据库,并将其放置在缓存中
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connection">一个合法的连接对象</param>
InBlock.gif        
/// <param name="spName">存储过程名</param>
InBlock.gif        
/// <param name="includeReturnValueParameter">是否在结果中返回参数值</param>
ExpandedSubBlockEnd.gif        
/// <returns>参数数组</returns>

InBlock.gif        private static SqlParameter[] GetSpParameterSetInternal(SqlConnection connection, string spName, bool includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (connection == nullthrow new ArgumentNullException("connection");
InBlock.gif            
if (spName == null || spName.Length == 0throw new ArgumentNullException("spName");
InBlock.gif
InBlock.gif            
string hashKey = connection.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter" : "");
InBlock.gif
InBlock.gif            SqlParameter[] cachedParameters;
InBlock.gif
InBlock.gif            cachedParameters 
= paramCache[hashKey] as SqlParameter[];
InBlock.gif            
if (cachedParameters == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlParameter[] spParameters 
= DiscoverSpParameterSet(connection, spName, includeReturnValueParameter);
InBlock.gif                paramCache[hashKey] 
= spParameters;
InBlock.gif                cachedParameters 
= spParameters;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return CloneParameters(cachedParameters);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 Parameter Discovery Functions
InBlock.gif
ExpandedBlockEnd.gif    }


//--如果您认真看的上面有个提问为什么要克隆- -这是利用原型设计模式,克隆一个拷贝然后直接用,如果您是用来做存储过程的实体~~就不必克隆了--省内存
//--接下来改写下- -原创阿 14.gif
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
namespace  Ajaxren
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 缓存参数用的
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SqlParametersCache : System.Collections.Generic.Dictionary<string, SqlParameter[]>
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static SqlParametersCache t = new SqlParametersCache();
InBlock.gif        
//--单例构造模式
InBlock.gif
        SqlParametersCache()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回对象的实例----因为本例是单例构造模式
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static SqlParametersCache GetSqlParametersContent
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return t;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 检索缓冲参数,检索的最终形式为ConnectionString,SqlCommandText
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ConnectionString">连接数据库的字符串</param>
InBlock.gif        
/// <param name="SqlCommandText">存储过程的名称</param>
InBlock.gif        
/// <param name="includeReturnValueParameter">是否包含一个ReturnValue的返回数据~~默认的~~讨厌~~~~</param>
ExpandedSubBlockEnd.gif        
/// <param name="return">SqlParameter[]返回一个参数集合</param>

InBlock.gif        public SqlParameter[] GetCatchSqlParameter(string ConnectionString, string SqlCommandText, bool includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string c = "";
InBlock.gif            
if (ConnectionString == nullthrow new ArgumentNullException("请输入你的连接字符串");
InBlock.gif            
if (SqlCommandText == null || SqlCommandText.Length == 0throw new ArgumentNullException("请输入你的存储过程名字");
InBlock.gif            
//--检查集合里面是否有指定数据
InBlock.gif
            if (this.ContainsKey(ConnectionString + ":" + SqlCommandText) != false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//--有数据的是在返回的时候在判断this[ConnectionString + ":" + SqlCommandText]key里面包含的数据如果不存在--调用this.GoToSqlSererSelectParameter在次找到该数据
InBlock.gif

InBlock.gif                
if (this[ConnectionString + ":" + SqlCommandText] == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//--再次缓冲这个对象咯~~-调用this.GoToSqlSererSelectParameter找到参数
InBlock.gif
                    this[ConnectionString + ":" + SqlCommandText] = this.GoToSqlSererSelectParameter(ConnectionString, SqlCommandText, includeReturnValueParameter);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
//--返回数据
InBlock.gif
                return Cps(this[ConnectionString + ":" + SqlCommandText]);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else//-不存在再次缓冲-----key--------------------------------------value-调用this.GoToSqlSererSelectParameter找到参数
InBlock.gif
                this.Add(ConnectionString + ":" + SqlCommandText, this.GoToSqlSererSelectParameter(ConnectionString, SqlCommandText, includeReturnValueParameter));
InBlock.gif
InBlock.gif            
return this[ConnectionString + ":" + SqlCommandText];
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 去Sql中检索存储过程的参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ConnectionString">检索的字符串</param>
InBlock.gif        
/// <param name="SqlCommandText">存储过程的名称</param>
ExpandedSubBlockEnd.gif        
/// <param name="includeReturnValueParameter">是否包含一个ReturnValue的返回数据~~默认的~~讨厌~~~~</param>

InBlock.gif        private SqlParameter[] GoToSqlSererSelectParameter(string ConnectionString, string SqlCommandText, bool includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            SqlConnection BugConnection 
= new SqlConnection(ConnectionString);
InBlock.gif            SqlCommand BugCommand 
= new SqlCommand(SqlCommandText, BugConnection);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                BugCommand.CommandType 
= CommandType.StoredProcedure;
InBlock.gif                BugConnection.Open();
InBlock.gif                
//----反向去SqlServer-找到参数----写入到--BugCommand里面
InBlock.gif
                SqlCommandBuilder.DeriveParameters(BugCommand);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (SqlException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                BugConnection.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif            
//---false不保存删除第一个参数--这个参数是ParameterDirection.ReturnValue类型
InBlock.gif
            if (!includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (BugCommand.Parameters[0].Direction == ParameterDirection.ReturnValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    BugCommand.Parameters.RemoveAt(
0);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//--是否包含返回参数     
InBlock.gif
            SqlParameter[] discoveredParameters = new SqlParameter[BugCommand.Parameters.Count];
InBlock.gif
InBlock.gif
InBlock.gif            
//---将参数拷贝到参数集合中--要拷贝到的参数集合--从指定目标数组索引处开始
InBlock.gif
            BugCommand.Parameters.CopyTo(discoveredParameters, 0);
InBlock.gif            
// Init the parameters with a DBNull value--将对象复空值
InBlock.gif
            foreach (SqlParameter discoveredParameter in discoveredParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    discoveredParameter.Value 
= DBNull.Value;
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (System.Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif            
return Cps(discoveredParameters);
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
//--作用创建对象的副本
InBlock.gif
        private static SqlParameter[] Cps(SqlParameter[] originalParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter[] clonedParameters 
= new SqlParameter[originalParameters.Length];
InBlock.gif
InBlock.gif            
for (int i = 0, j = originalParameters.Length; i < j; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//----复制构造函数,以便使用当前实例的值初始化Parameter 类的新实例。
InBlock.gif

InBlock.gif                clonedParameters[i] 
= (SqlParameter)((ICloneable)originalParameters[i]).Clone();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return clonedParameters;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/ajaxren/archive/2007/05/19/752644.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值