在C#中怎么调用存储过程?新手刚接触存储过程,求指教
在C#中如何调用存储过程??新手刚接触存储过程,求指教
贴上存储过程的代码:
请问我应该怎么调用这个存储过程呢。。。通过这个存储过程进行更新插入的操作。最好可以给出详细的调用代码。。。
------解决方案--------------------
http://blog.luohuedu.net/blog/31802.aspx
------解决方案--------------------
贴上存储过程的代码:
ALTER proc [dbo].[tt_buttonSql]
@ID int,
@ButtonCode varchar(50),
@ButtonName varchar(50),
@orderno int,
@tag varchar(255),
@rtn int output --//output选项指示参数是输出参数
as
--as前面声明的是传给存储过程的
declare--declare定义的是存储过程内部使用的变量
@tmpID int,
@tmpButtonCode varchar(50),
@tmpButtonName varchar(50),
@tmporderno int,
@tmptag varchar(255)
if exists(select * from tt_button where ID=@ID)
begin
select @tmpButtonCode=ButtonCode,@tmpButtonName=ButtonName,@tmporderno=orderno,@tmptag=tag from tt_button where ID=@ID
if((@tmpButtonCode=@ButtonCode)and(@tmpButtonName=@ButtonName)and(@tmporderno=@orderno)and(@tmptag=@tag))
begin
set @rtn=0--有相同的数据,直接返回值
end
else
begin
update tt_button set ID=@ID,ButtonCode=@ButtonCode,ButtonName=@ButtonName,orderno=@orderno,tag=@tag where ID=@ID
set @rtn=2--有主键相同的数据,进行更新处理
end
end
else
begin
insert into tt_button values(@ID,@ButtonCode,@ButtonName,@orderno,@tag)
set @rtn=1--没有相同的数据,进行插入处理
end
GO
请问我应该怎么调用这个存储过程呢。。。通过这个存储过程进行更新插入的操作。最好可以给出详细的调用代码。。。
------解决方案--------------------
http://blog.luohuedu.net/blog/31802.aspx
------解决方案--------------------
int value = 0;
using (SqlConnection myConnection = new SqlConnection("数据库连接的字符串"))
{
myConnection.Open();
using (SqlCommand myCommand = new SqlCommand("dbo.tt_buttonSql", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@ID", SqlDbType.Int);
myCommand.Parameters["@ID"].Value = 1;
myCommand.Parameters.Add("@ButtonCode", SqlDbType.VarChar);
myCommand.Parameters["@ButtonCode"].Value = "code";
myCommand.Parameters.Add("@ButtonName", SqlDbType.VarChar);
myCommand.Parameters["@ButtonName"].Value = "name";
myCommand.Parameters.Add("@orderno", SqlDbType.Int);
myCommand.Parameters["@orderno"].Value = 1;
myCommand.Parameters.Add("@tag", SqlDbType.VarChar);
myCommand.Parameters["@tag"].Value = "tag";
myCommand.Parameters.Add("@rtn", SqlDbType.Int);
myCommand.Parameters["@rtn"].Direction = ParameterDirection.Output;
myCommand.ExecuteNonQuery();