asp.net中怎样调用存储过程和存储过程的写法
2011-02-18 16:01
645 查看
asp.net中怎样调用存储过程和存储过程的写法
创建一个只有输入参数的存储过程
create procedure proc_user
@name varchar(20),
@Password varchar(100)
as
select * from loginuser
where name like @name
---创建一个有输入和输出参数的存储过程
create procedure proc_usertext
@name varchar(20),
@blog varchar(100) output
as
select @blog = blog from loginuser where name = @name
---创建一个有输入和返回参数的存储过程
create PROCEDURE returnval
@name varchar(100),
@blog varchar(100) output
AS
begin
select @blog = blog from loginuser where name = @name
if(@blog is null)
set @blog = ''你还没有申请博客''
return execute(@blog ) --数据数型的转换
end
在asp.net中的调用
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
private void DataBind()
{
//只带输入参数的存储过程的写法。
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("proc_user", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", "l%");
cmd.Parameters.AddWithValue("@Password", "lcs9702");
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList1.DataSource = dr;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "use_id";
DropDownList1.DataBind();
}
private void textDataBind()
{
//带输入,输出参数的存储过程的写法。
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("proc_usertext", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@name", "love");
cmd.Parameters.Add("@blog", SqlDbType.VarChar,100);
cmd.Parameters["@blog"].Direction = ParameterDirection.Output;
//cmd.Parameters.Add(new SqlParameter("@blog",SqlDbType.VarChar,100,ParameterDirection.Output,false,0,0,string.Empty,DataRowVersion.Default,null));
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
TextBox1.Text = cmd.Parameters["@blog"].Value.ToString();
con.Close();
}
private void returnDataBind()
{
//带输入,返回参数的存储过程的写法。
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("returnval", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@name", "lcs9702");
cmd.Parameters.Add(new SqlParameter("@blog", SqlDbType.VarChar, 100, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, null));
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
TextBox2.Text = cmd.Parameters["@blog"].Value.ToString();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
textDataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
returnDataBind();
}
创建一个只有输入参数的存储过程
create procedure proc_user
@name varchar(20),
@Password varchar(100)
as
select * from loginuser
where name like @name
---创建一个有输入和输出参数的存储过程
create procedure proc_usertext
@name varchar(20),
@blog varchar(100) output
as
select @blog = blog from loginuser where name = @name
---创建一个有输入和返回参数的存储过程
create PROCEDURE returnval
@name varchar(100),
@blog varchar(100) output
AS
begin
select @blog = blog from loginuser where name = @name
if(@blog is null)
set @blog = ''你还没有申请博客''
return execute(@blog ) --数据数型的转换
end
在asp.net中的调用
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
private void DataBind()
{
//只带输入参数的存储过程的写法。
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("proc_user", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", "l%");
cmd.Parameters.AddWithValue("@Password", "lcs9702");
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList1.DataSource = dr;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "use_id";
DropDownList1.DataBind();
}
private void textDataBind()
{
//带输入,输出参数的存储过程的写法。
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("proc_usertext", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@name", "love");
cmd.Parameters.Add("@blog", SqlDbType.VarChar,100);
cmd.Parameters["@blog"].Direction = ParameterDirection.Output;
//cmd.Parameters.Add(new SqlParameter("@blog",SqlDbType.VarChar,100,ParameterDirection.Output,false,0,0,string.Empty,DataRowVersion.Default,null));
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
TextBox1.Text = cmd.Parameters["@blog"].Value.ToString();
con.Close();
}
private void returnDataBind()
{
//带输入,返回参数的存储过程的写法。
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("returnval", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@name", "lcs9702");
cmd.Parameters.Add(new SqlParameter("@blog", SqlDbType.VarChar, 100, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, null));
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
TextBox2.Text = cmd.Parameters["@blog"].Value.ToString();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
textDataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
returnDataBind();
}
相关文章推荐
- asp.net中怎样调用存储过程和存储过程的写法
- asp.net中怎样调用存储过程和存储过程的写法
- asp.net中怎样调用存储过程和存储过程的写法
- asp.net中怎样调用存储过程和存储过程的写法
- asp.net中怎样调用存储过程和存储过程的写法(转载,留着自己看)
- C# asp.net SQL存储过程写法Insert 插入语句,及其在asp.net中的调用方法
- 解析在ASP.NET中调用存储过程的方法
- asp.net中IDataParameter调用存储过程的实现方法
- 千万级数据分页存储过程 asp.net页面调用分页全过程
- asp.net SQL Server 存储过程分页及代码调用
- asp.net调用存储过程2
- sql 存储过程 几个 简单例子,及在asp.net(C#)中调用存储过程的代码
- asp.net中调用存储过程的方法
- asp.net 调用 mysql 分页存储过程,repeater使用mysql分页存储过程,mysql 存储过程的注意事项
- ASP.NET如何调用MySQL的存储过程
- 【ASP.NET开发】ADO.NET调用带输出参数的存储过程 分类: ASP.NET 2012-09-10 21:16 1261人阅读 评论(1) 收藏
- [Database]创建存储过程并在ASP.NET中的调用带参数的存储过程与及调用带参数的SQL语句对比
- 关于asp.net 里面调用存储过程里面的output解答 试过的
- 在ASP.net中调用存储过程的代码
- ASP.NET中批量删除--只调用一次存储过程