private void buycatalog()
{
SqlConnection conndb= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
conndb.Open();
SqlDataAdapter strselect = new SqlDataAdapter("productclass",conndb);
strselect.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
strselect.Fill(ds);
DlstBuycatalog.DataSource =ds;
DlstBuycatalog.DataKeyField ="PdtCat_ID";
DlstBuycatalog.DataBind();
conndb.Close();
}
以上这个方法,就是通过SqlDataAdapter对像调用了SQL中存储过程productclass,通过DataSet将数据填充在ds中,同时指定DataList控件DlstBuycatalog的数据源是ds,主键是PdtCat_Id,最后再重新绑定Datalist控件.由这个方法我们可以看到用SqlDataAdapter调用存储过程中的关键是:
SqlDataAdapter strselect = new SqlDataAdapter("productclass",conndb);
strselect.SelectCommand.CommandType = CommandType.StoredProcedure;
当存储过程中有参数时,我们又应该乍样做呢?其实这个跟SqlCommand的差不多,我们只要再加一句
Strselect.SelectCommand.Parameter.Add(“@pdt_name”,txtpdtname.Text());
就可以了,其中@pdt_name是在存储过程中声明的参数变量名,而txtpdtname.text()是在.net中赋于变量@pdt_name的值了。认真看一下下面这个存储过程就很清楚了:
由上面我们可以知道在调用存储过程中,最关键的对象是Command对象,这个对象可以通过ExecuteReader()方法执行数据查询,还可以返回一个单一值的查询,还可以通过ExecuteScalar()方法进行相关的数据统计,还可以通过ExecuteNonQuery()方法进行数据更新,增删改的执行操作,而在执行这些SQL操作时,往往是与相关的控件DataGrid ,DataList,Repeat控件结合使用的.
(3)常用的一些存储过程例子
以下是自己在最近所做的一个项目中所用到的一些存储过程,可能由于自己水平有限,有些写得不是很规范,不过大部分都实现到我想要的结果了,这些存储过程都可以正常执行,把这些发出来给大家(数据库因保密请见谅),希望对大家用用,同时希望指正其中的错误,谢谢。
(1) 选择所有的记录
程序代码:
/*
作者:德仔
用途:查询sellinfo里所有的记录
日期:2006-3-23
*/
create procedure sellinfo_select
as
select * from sellinfo
GO
(2) 删除指定的ID记录
程序代码:
/*
作者:德仔
用途:删除sellinfo里由输入参数@sell_id指定的ID记录
日期:2006-3-23
*/
CREATE PROCEDURE sellinfo_delete
@sell_id bigint
as
delete from [sellinfo]
where
sell_id=@sell_id
GO
(3)更新所对应的记录
程序代码:
/*
作者:德仔
用途:修改相对应的小类名
日期:2006-4-5
*/
create procedure prosmallclass_update_id








