《.NET中统一的存储过程调用方法(收藏) 》的具体实现

网友询问转载文章中.NET统一存储过程调用方法的实现,博主编写代码完成此功能。代码中有两个类,一个用于返回查询数据和存储过程返回值、输出参数,调用时参数为可变参数。不过代码仅为测试,存在小问题。

最近有个网友问我转载的文章《.NET中统一的存储过程调用方法(收藏) 》具体该如何实现.

于是我写了代码完成了此功能.代码中有两个类

一个是用来做返回值的载体的用一个DataSet返回查询出的数据,用一个Hashtable返回存储过程的返回值和输出参数.

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  DDLLY
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// SqlResult 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SqlResult 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public DataSet MyDataSet=new DataSet();
InBlock.gif        
public Hashtable ReturnVal=new Hashtable();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

这个类是具体的实现,代码并不复杂,这里我也就不罗嗦了.只需要注意方法的参数是可变参数.

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  DDLLY
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// SQLProcHelper 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SQLProcHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//存储过程名
InBlock.gif
        private String procedureName=null;
InBlock.gif        
//连接字符串
InBlock.gif
        private String connectionString=null;
InBlock.gif
InBlock.gif        
private SqlConnection myConnection=new SqlConnection();
InBlock.gif        
private SqlCommand myCommand=new SqlCommand();
InBlock.gif        
private SqlParameter myParameter=new SqlParameter();
InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
存储过程名#region 存储过程名
InBlock.gif        
public String ProcedureName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return procedureName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                procedureName
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
连接字符串#region 连接字符串
InBlock.gif        
public String ConnectionString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return connectionString;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                connectionString
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造函数#region 构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public SQLProcHelper()dot.gif{}
InBlock.gif
InBlock.gif        
public SQLProcHelper(String ProcedureName,String ConnectionString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            procedureName
=ProcedureName;
InBlock.gif            connectionString
=ConnectionString;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
调用存储过程#region 调用存储过程
InBlock.gif        
public SqlResult Call(params object[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// SqlResult是自己定义的用于保存结果数据集、返回值、传出参数集的类
InBlock.gif
            SqlResult result = new SqlResult();
InBlock.gif
InBlock.gif            
// 根据需要定义自己的连接字符串
InBlock.gif
            myConnection  = new SqlConnection(ConnectionString);
InBlock.gif
InBlock.gif            myCommand 
= new SqlCommand(this.ProcedureName, myConnection);
InBlock.gif            myCommand.CommandType 
= CommandType.StoredProcedure;
InBlock.gif
InBlock.gif            SqlDataAdapter myAdapter 
= new SqlDataAdapter(myCommand);
InBlock.gif
InBlock.gif            myConnection.Open();
InBlock.gif            
// 获得和创建存储过程的参数,并且设置好值
InBlock.gif
            GetProcedureParameter(parameters);
InBlock.gif            myAdapter.Fill(result.MyDataSet, 
"Table");
InBlock.gif    
InBlock.gif            
// 获得存储过程的传出参数值和名字对,保存在一个Hashtable中
InBlock.gif
            GetOutputValue(result);
InBlock.gif
InBlock.gif            
// 在这里释放各种资源,断开连接
InBlock.gif
            myAdapter.Dispose();
InBlock.gif            myCommand.Dispose();
InBlock.gif            myConnection.Close();
InBlock.gif            myConnection.Dispose();
InBlock.gif
InBlock.gif
InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获得存储过程的参数#region 获得存储过程的参数
InBlock.gif        
private void GetProcedureParameter(params object[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlDataReader reader 
= null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlCommand myCommand2 
= new SqlCommand();
InBlock.gif 
InBlock.gif                myCommand2.Connection 
= this.myConnection;
InBlock.gif                myCommand2.CommandText 
= "select PARAMETER_NAME,PARAMETER_MODE,DATA_TYPE from INFORMATION_SCHEMA.PARAMETERS where SPECIFIC_NAME='" +this.ProcedureName+ "' order by ORDINAL_POSITION";
InBlock.gif 
InBlock.gif                reader 
= myCommand2.ExecuteReader();
InBlock.gif        
InBlock.gif 
InBlock.gif                
// 创建返回参数
InBlock.gif
                myParameter = new SqlParameter();
InBlock.gif                myParameter.ParameterName 
= "@Value";
InBlock.gif                myParameter.SqlDbType 
= SqlDbType.Int;
InBlock.gif                myParameter.Direction 
= ParameterDirection.ReturnValue;
InBlock.gif 
InBlock.gif                myCommand.Parameters.Add(myParameter);
InBlock.gif 
InBlock.gif                
int i = 0;
InBlock.gif                
// 创建各个参数,在这个地方可以自动的创建SqlParameter的类型,值,方向等属性
InBlock.gif
                while(reader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    myParameter 
= new SqlParameter();
InBlock.gif 
InBlock.gif                    myParameter.ParameterName 
= reader["PARAMETER_NAME"].ToString();
InBlock.gif                    myParameter.Direction 
= reader["PARAMETER_MODE"].ToString()=="IN"?ParameterDirection.Input:ParameterDirection.Output;
InBlock.gif        
InBlock.gif 
InBlock.gif                    
switch(reader["DATA_TYPE"].ToString().ToUpper())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
case "NVARCHAR":
InBlock.gif                            myParameter.SqlDbType 
=SqlDbType.NVarChar;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(string)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "VARCHAR":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.VarChar;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(string)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "BIT":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Bit;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(bool)parameters[i];    
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "BIGINT":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.BigInt;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{    
InBlock.gif                                myParameter.Value
=(int)parameters[i];    
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "CHAR":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Char;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(string)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "DATETIME":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.DateTime;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(DateTime)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "DECIMAL":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Decimal;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(Decimal)parameters[i];        
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "FLOAT":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Float;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(float)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                            
//                    case "IMAGE":
InBlock.gif                            
//                        myParameter.SqlDbType = SqlDbType.Image;
InBlock.gif                            
//                        break;
InBlock.gif
                        case "INT":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Int;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(int)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "MONEY":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Money;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(Decimal)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "NCHAR":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.NChar;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(string)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "NTEXT":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.NText;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(string)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "REAL":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Real;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(Double)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "TEXT":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Text;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(string)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                            
//                    case "VARBINARY":
InBlock.gif                            
//                        myParameter.SqlDbType = SqlDbType.VarBinary;
InBlock.gif                            
//                        break;
InBlock.gif
                        case "SMALLDATETIME":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.SmallDateTime;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(DateTime)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "SMALLINT":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.SmallInt;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(int)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "SMALLMONEY":
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.SmallMoney;
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                myParameter.Value
=(int)parameters[i];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                            
//                    case "TIMESTAMP":
InBlock.gif                            
//                        myParameter.SqlDbType = SqlDbType.Timestamp;
InBlock.gif                            
//                        break;
InBlock.gif
                        default:
InBlock.gif                            
throw new Exception("不支持的数据类型!");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    i
++;
InBlock.gif 
InBlock.gif                    myCommand.Parameters.Add(myParameter);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

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

InBlock.gif 
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获得返回值参数#region 获得返回值参数
InBlock.gif        
private void GetOutputValue(SqlResult result)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//遍历所有参数
InBlock.gif
            foreach (SqlParameter para in myCommand.Parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//如果是返回参数或者输出参数
InBlock.gif
                if (para.Direction!=ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    result.ReturnVal.Add(para.ParameterName,para.Value);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

调用的时候只需要把参数直接做方法的参数即可.

None.gif      try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                SQLProcHelper helper
=new SQLProcHelper("byroyalty","server=ddl;database=pubs;uid=sa;pwd=wd");
InBlock.gif                SqlResult result
=helper.Call(100);
InBlock.gif                dataGrid1.DataSource
=result.MyDataSet.Tables[0];
ExpandedBlockEnd.gif            }

None.gif            
catch (Exception ex)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                label1.Text
=ex.Message;
ExpandedBlockEnd.gif            }

由于这段代码只是做个测试,所以还存在不少小问题.

转载于:https://www.cnblogs.com/renrenqq/archive/2004/08/04/30143.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值