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

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

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

  CreateCommand创建命令

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

        private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)
        
{
            
if ((commandParameters == null|| parameterValues == null)
            
{
                
//如果没有数据则返回
                return;
            }


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


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

                    
else
                    
{
                        commandParameters[i].Value 
= paraInstance.Value;
                    }

                }

                
else if (parameterValues[i] == null)
                
{
                    commandParameters[i].Value 
= DBNull.Value;
                }

                
else
                
{
                    commandParameters[i].Value 
= parameterValues[i];
                }

            }

        }



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

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

   /// <summary>
    
/// SqlHelperParameterCache 提供一些函数用来发现存储过程的参数
    
/// </summary>

    internal sealed class SqlHelperParameterCache
    
{
        
private methods, variables, and constructors private methods, variables, and constructors

        
caching functions caching functions

        
Parameter Discovery Functions Parameter Discovery Functions

    }


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

    
/// <summary>
    
/// 缓存参数用的
    
/// </summary>

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


        
/// <summary>
        
/// 返回对象的实例----因为本例是单例构造模式
        
/// </summary>

        public static SqlParametersCache GetSqlParametersContent
        
{
            
get
            
{
                
return t;
            }

        }

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

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

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

                
//--返回数据
                return Cps(this[ConnectionString + ":" + SqlCommandText]);

            }

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

            
return this[ConnectionString + ":" + SqlCommandText];

        }


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

        private SqlParameter[] GoToSqlSererSelectParameter(string ConnectionString, string SqlCommandText, bool includeReturnValueParameter)
        
{

            SqlConnection BugConnection 
= new SqlConnection(ConnectionString);
            SqlCommand BugCommand 
= new SqlCommand(SqlCommandText, BugConnection);
            
try
            
{
                BugCommand.CommandType 
= CommandType.StoredProcedure;
                BugConnection.Open();
                
//----反向去SqlServer-找到参数----写入到--BugCommand里面
                SqlCommandBuilder.DeriveParameters(BugCommand);
            }

            
catch (SqlException)
            
{

            }

            
finally
            
{

                BugConnection.Close();
            }



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

            }


            
//--是否包含返回参数     
            SqlParameter[] discoveredParameters = new SqlParameter[BugCommand.Parameters.Count];


            
//---将参数拷贝到参数集合中--要拷贝到的参数集合--从指定目标数组索引处开始
            BugCommand.Parameters.CopyTo(discoveredParameters, 0);
            
// Init the parameters with a DBNull value--将对象复空值
            foreach (SqlParameter discoveredParameter in discoveredParameters)
            
{
                
try
                
{
                    discoveredParameter.Value 
= DBNull.Value;

                }

                
catch (System.Exception)
                
{

                }

            }



            
return Cps(discoveredParameters);



        }





        
//--作用创建对象的副本
        private static SqlParameter[] Cps(SqlParameter[] originalParameters)
        
{
            SqlParameter[] clonedParameters 
= new SqlParameter[originalParameters.Length];

            
for (int i = 0, j = originalParameters.Length; i < j; i++)
            
{
                
//----复制构造函数,以便使用当前实例的值初始化Parameter 类的新实例。

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


            
return clonedParameters;
        }

    }

}
posted @ 2007-05-19 19:52  苹果王子  阅读(8327)  评论(24编辑  收藏  举报