日期:2011-10-04 浏览次数:20570 次
最近在csdn上遇到些朋友在问在asp.net上调用存储过程的方法,在这里将我的经验总结一下并整理发布处理,供大家参考。
基本思路是:先获得存储过程的参数,然后根据参数表收集值,然后再调用存储过程。但要求在页面中的控件id必须与存储过程的参数保持一致。并有几种调用方式。这个也是经验的总结,并未仔细的推敲,包括很多地方没有捕捉error,如果有朋友将起改进,麻烦将副本发给我一个,谢谢。本人mail:huangguolinc@163.com
public class DB:Page
{
//数据库连接
public SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Conn"]);
//创建SqlCommand对象
private SqlCommand cmd;
public SqlDataReader returnsdr;
public string sqlQueryString="";
public string SQS
{
set
{
sqlQueryString=value;
}
get
{
return sqlQueryString;
}
}
public SqlDataReader SDR
{
set
{
returnsdr=value;
}
get
{
return returnsdr;
}
}
public string[] paras={};
public string[] values={};
public string valuetype="ds";
public string ValueType
{
set
{
valuetype=value;
}
get
{
return valuetype;
}
}
public string[] Paras
{
set
{
paras=value;
}
get
{
return paras;
}
}
public string[] Values
{
set
{
values=value;
}
get
{
return values;
}
}
public System.Web.UI.HtmlControls.HtmlForm hf;
public System.Web.UI.HtmlControls.HtmlForm HF{set{hf=value;}get{return hf;}}
public bool hfEnable=true;
public bool HFEnable{set {hfEnable=value;}get{return hfEnable;}}
public int info;
public int Info{set{info=value;}get{return info;}}
//连接数据库
public SqlConnection Conn()
{
if(conn.State!=ConnectionState.Open)
{
conn.Open();
}
return conn;
}
//获取存储过程的参数
protected DataSet GetStoreProcedureParams(string StoreProcedureName)
{
conn=this.Conn();
int StoreProcedureId=-1;
DataSet ds=new DataSet();
SqlCommand sc=new SqlCommand("SELECT id FROM dbo.sysobjects WHERE name = '"+StoreProcedureName+"'",conn);
SqlDataReader sdr=sc.ExecuteReader();
while(sdr.Read())
{
StoreProcedureId=sdr.GetInt32(0);
}
sdr.Close();
SqlDataAdapter sda=new SqlDataAdapter("SELECT dbo.syscolumns.name, dbo.systypes.name AS type, dbo.syscolumns.length,dbo.syscolumns.isoutparam FROM dbo.syscolumns INNER JOIN dbo.systypes ON dbo.syscolumns.xtype = dbo.systypes.xtype WHERE dbo.syscolumns.id ='"+StoreProcedureId+"'",conn);
sda.Fill(ds,"dbo.syscolumns");
//sda.Fill(ds,"dbo.systypes");
return ds;
}
public SqlCommand CallStoreProcedure(string StoreProcedureName)
{
//Server.Transfer("../main/1.aspx");
//连接数据库<