C#调用存储过程的类

using  System;
None.gif
using  System.IO;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
None.gif
namespace  Forum.Component
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif
InBlock.gif    
///     存储过程的返回值纪录类
InBlock.gif    
///     DataSet : 表示返回的表
InBlock.gif    
///     Output  : 存储过程的输出参数
InBlock.gif    
///     Value   : 存储过程的返回值
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SqlResult
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int Value;
InBlock.gif        
public Hashtable Output;
InBlock.gif        
public DataSet dataSet;
InBlock.gif
InBlock.gif        
public SqlResult()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Value 
= 0;
InBlock.gif            Output 
= new Hashtable();
InBlock.gif            dataSet 
= new DataSet();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif
InBlock.gif    
///     用于调用数据库中的存储过程,返回一个DataSet、Output、Value的SqlResult类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SqlProcedure
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string sp_name;
InBlock.gif        
private SqlConnection myConnection;
InBlock.gif        
private SqlCommand myCommand;
InBlock.gif        
private SqlParameter myParameter;
InBlock.gif
InBlock.gif        
public string ProcedureName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn this.sp_name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gifthis.sp_name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SqlProcedure() : this("")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SqlProcedure(string sp_name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ProcedureName = sp_name;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public SqlResult Call(params object[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlResult result 
= new SqlResult();
InBlock.gif
InBlock.gif            myConnection  
= new SqlConnection(ConfigurationSettings.AppSettings["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            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myConnection.Open();
InBlock.gif
InBlock.gif                GetProcedureParameter(parameters);
InBlock.gif
InBlock.gif                myAdapter.Fill(result.dataSet, 
"Table");
InBlock.gif
InBlock.gif                GetOutputValue(result);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw e;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myAdapter.Dispose();
InBlock.gif                myCommand.Dispose();
InBlock.gif                myConnection.Close();
InBlock.gif                myConnection.Dispose();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void GetProcedureParameter(params object[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCommand myCommand2 
= new SqlCommand();
InBlock.gif
InBlock.gif            myCommand2.Connection 
= this.myConnection;
InBlock.gif            myCommand2.CommandText 
= "select * from INFORMATION_SCHEMA.PARAMETERS where SPECIFIC_NAME='" +this.ProcedureName+ "' order by ORDINAL_POSITION";
InBlock.gif
InBlock.gif            SqlDataReader reader 
= null;
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                reader 
= myCommand2.ExecuteReader();
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                
int i = 0;
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                    
switch(reader["DATA_TYPE"].ToString())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
case "bit" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (bool)parameters[i];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Bit;
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
case "bigint" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (int)parameters[i];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.BigInt;
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
case "int" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                    myParameter.Value 
= (int)parameters[i];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Int;
InBlock.gif                            
break;
InBlock.gif                            
InBlock.gif                        
case "decimal" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (double)parameters[i];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Decimal;
InBlock.gif                            myParameter.Precision 
= (byte)reader["NUMERIC_PRECISION"];
InBlock.gif                            myParameter.Scale 
= (byte)reader["NUMERIC_SCALE"];
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
case "nvarchar" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (string)parameters[i];
InBlock.gif                            myParameter.Size 
= (int)reader["CHARACTER_MAXIMUM_LENGTH"];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.NVarChar;
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
case "varchar" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (string)parameters[i];
InBlock.gif                            myParameter.Size 
= (int)reader["CHARACTER_MAXIMUM_LENGTH"];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.VarChar;
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
case "nchar" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (string)parameters[i];
InBlock.gif                            myParameter.Size 
= (int)reader["CHARACTER_MAXIMUM_LENGTH"];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.NChar;
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
case "char" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (string)parameters[i];
InBlock.gif                            myParameter.Size 
= (int)reader["CHARACTER_MAXIMUM_LENGTH"];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Char;
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
case "ntext" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (string)parameters[i];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.NText;
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
case "text" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (string)parameters[i];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Text;
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
case "datetime" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (DateTime)parameters[i];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.DateTime;
InBlock.gif                            
break;
InBlock.gif                        
case "smalldatetime" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
InBlock.gif                                myParameter.Value 
= (DateTime)parameters[i];
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.DateTime;
InBlock.gif                            
break;
InBlock.gif                        
case "image" :
InBlock.gif                            
if(myParameter.Direction == ParameterDirection.Input)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                HttpPostedFile PostedFile 
= (HttpPostedFile)parameters[i];
InBlock.gif
InBlock.gif                                Byte[] FileByteArray 
= new Byte[PostedFile.ContentLength];
InBlock.gif                                Stream StreamObject 
= PostedFile.InputStream;
InBlock.gif                                StreamObject.Read(FileByteArray,
0,PostedFile.ContentLength);
InBlock.gif
InBlock.gif                                myParameter.Value 
= FileByteArray;
ExpandedSubBlockEnd.gif                            }

InBlock.gif
InBlock.gif                            myParameter.SqlDbType 
= SqlDbType.Image;
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
case "uniqueidentifier" :
InBlock.gif                            
//myParameter.Value = (string)parameters[i];
InBlock.gif
                            myParameter.SqlDbType = SqlDbType.UniqueIdentifier;
InBlock.gif                            
break;
InBlock.gif
InBlock.gif                        
default : break;
ExpandedSubBlockEnd.gif                    }

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

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw e;
InBlock.gif            
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(reader!=null) reader.Close();
InBlock.gif                myCommand2.Dispose();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private void GetOutputValue(SqlResult result)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            result.Value 
= (int)myCommand.Parameters["@Value"].Value;
InBlock.gif
InBlock.gif            
foreach(SqlParameter parameter in myCommand.Parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(parameter.Direction == ParameterDirection.Output)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    result.Output.Add(parameter.ParameterName, parameter.Value);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

 

调用方法

None.gif using  Forum.Component
None.gif
protected  SqlProcedure procedure = new  SqlProcedure();
None.gif
public  SqlResult result;
None.gif
None.gifprocedure.ProcedureName
= " sp_name " ;
None.gifresult
= procedure.Call( 1 , " 1 " , " 2 " );
None.gif

posted on 2007-07-04 23:22 ffan 阅读(508) 评论(2)  编辑 收藏 引用 所属分类: .NET

29474.html?webview=1

评论

 re: [.NET][C#][转载]调用存储过程的类 2007-10-25 22:46

有个问题
如果存储过程有返回值 如何取到?
yuwy.sh@163.com给个消息.
       

 re: [.NET][C#][转载]调用存储过程的类 2007-11-28 17:25

@tset

返回值在result 里

比如
public SqlResult result;

procedure.ProcedureName="sp_name";
result=procedure.Call(1,"1","2");
int abc = (int)result.Output["aaa"];    

转载于:https://www.cnblogs.com/jh0262/archive/2008/09/18/2946798.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值