打印

[asp.net教程] asp.net中如何调用存储过程

asp.net中如何调用存储过程

用asp.net与sql server可是缘份最好了,稍大的程序一般第一先考虑的是sql server,只是一些很考虑经济的才使用access等了。用sql server,为了使数据库的效率更好,一般都会才取存储过程,因存储过程执行速度快,并且可以实现一些高级的查询等功能。比如传入一些数据参数,但执行的sql过程可能不同等。

    下面就来个例子,建立一新的角色,要求角色的名字不能重复,以下是一存储过程。   
   
  create procedure sp_accountrole_create@categoryid int,@rolename nvarchar(10),@description nvarchar(50),@roleid int outputas declare @count int -- 查找是否有相同名称的记录 select @count = count(roleid) from account_role where rolename = @rolename if @count = 0 insert into account_role (categoryid, rolename, description) values (@categoryid, @rolename, @description) set @roleid = @@identity return 1go    执行存储过程的c#过程:
  sqlconnection dbconnection = new sqlconnection(mconnectionstring);sqlcommand command = new sqlcommand( "sp_accountrole_create", dbconnection );dbconnection.open(connectstring);// 废置sqlcommand的属性为存储过程command.commandtype = commandtype.storedprocedure;command.parameters.add("@categoryid", sqldbtype.int, 4);command.parameters.add("@rolename", sqldbtype.nvarchar, 10);command.parameters.add("@description", sqldbtype.nvarchar, 50);command.parameters.add("@roleid", sqldbtype.int, 4);// 返回值command.parameters.add("returnvalue", sqldbtype.int, 4, // size parameterdirection.returnvalue, false, // is nullable 0, // byte precision 0, // byte scale string.empty, datarowversion.default, null );command.parameters["@categoryid"].value = permission.categoryid;command.parameters["@rolename"].value = permission.permissionname;command.parameters["@description"].value = permission.description;// 可以返回新的id值command.parameters["@roleid"].direction = parameterdirection.output;int rowsaffected = command.executenonquery();int result = command.parameters["returnvalue"].value;int newid = command.parameters["@roleid"].value;
    功能挺强的吧,可以得到三个值,分别是行影响值,存储过程返回值,新的id值。
   
   
   
   
  trackback: http://tb.blog.csdn.net/trackback.aspx?postid=1487449

TOP

返回顶部
AYBlue

Processed in 0.059692 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2008-11-22 16:55 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP