关于asp.net 里面调用存储过程里面的output解答 试过的
1 添加表为:
create table user_login (id varchar(30),zhanghao varchar(30),mima varchar(30),quanxian varchar(30))
2 添加存储过程为:
alter proc proc_login_select
@zhanghao varchar (20)=null, --定义人员账号
@mima varchar (15)=null, --定义账号密码
@quanxian nvarchar(50)=null output --定义人员权限
as
begin
if exists(select id from user_login where zhanghao=@zhanghao and mima=@mima)--如果登录成功
select @quanxian=id from user_login where zhanghao=@zhanghao --查询权限付给@quanxian
else
set @quanxian='none' --否则返回
end
GO
3、asp.net数据处理:
em;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using house.lei_cs;
namespace house
{
public partial class ceshi : Form
{
public ceshi()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"server=zf\sql2000;pwd=fittec;uid=sa;pwd=1;database=house_zf");
SqlCommand cmd = new SqlCommand("proc_login_select", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection.Open();
SqlParameter[] prams =
{
new SqlParameter("@zhanghao", SqlDbType.VarChar, 50),
new SqlParameter("@mima", SqlDbType.VarChar, 50),
new SqlParameter("@quanxian", SqlDbType.VarChar, 50, ParameterDirection.Output,true, 0, 0, string.Empty,DataRowVersion.Default, null)
};
prams[0].Value = "001";
prams[1].Value = "123";
// 添加参数
foreach (SqlParameter parameter in prams)
{
cmd.Parameters.Add(parameter);
}
cmd.ExecuteNonQuery();
string strResult = cmd.Parameters["@quanxian"].Value.ToString();
conn.Close();
label1.Text = strResult.ToString();
}
}
}
注意事项,改了 output的变量,记得还要改一个:倒数第三行还有一个它的变量哦。
------------------------------------好理解的一种方式-----------------------------------------
//建立连接
SqlConnection conn = new SqlConnection(@"server=zf\sql2000;pwd=fittec;uid=sa;pwd=1;database=house_zf");
SqlCommand cmd = new SqlCommand("proc_login_select",conn );
cmd.CommandType = CommandType.StoredProcedure;
//定义参数
SqlParameter zhanghao = cmd.Parameters.Add("@zhanghao", SqlDbType.VarChar);
SqlParameter mima = cmd.Parameters.Add("@mima", SqlDbType.VarChar);
SqlParameter quanxian = cmd.Parameters.Add("@quanxian", SqlDbType.VarChar);
//指定参数方向
zhanghao.Direction = ParameterDirection.Input;
mima.Direction = ParameterDirection.Input;
quanxian.Direction = ParameterDirection.ReturnValue;
//赋值
zhanghao.Value = "001";
mima.Value ="123";
//执行
conn.Open();
cmd.ExecuteNonQuery();
label1.Text = quanxian.Value.ToString();
conn.Close();
posted on 2010-08-01 03:03 永不言弃program 阅读(337) 评论(0) 编辑 收藏 举报