终于弄明白存储过程怎么得到返回的参数了
以发一篇文章为例
有一个sql存储过程名叫Add内容是
USE [News]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
------------------------------------
--用途:增加一条记录
--项目名称:
--说明:
--时间:2013/10/22 10:14:34
------------------------------------
ALTER PROCEDURE [dbo].[NewsTable_ADD]
@ID int output,
@Title nvarchar(100),
@Content nvarchar(500)
AS
INSERT INTO [NewsTable](
[Title],[Content]
)VALUES(
@Title,@Content
)
SET @ID = @@IDENTITY
//C#文件内容是
SqlParameter[] parameters = {
new SqlParameter("@Id", SqlDbType.Int),
new SqlParameter("@Title", "tbTitle.Text"),
new SqlParameter("@Content", "tbContent.Text")
};
//将第一个outPut参数Direction参数设置为ParameterDirection.Output
parameters[0].Direction = ParameterDirection.Output;
parameters[1].Value = tbTitle.Text;
parameters[2].Value = "内容";
int dt = SQLHelper.ExecuteStoringProcess("ADD", parameters);
//sqlHelper的执行命令是
public static int ExecuteStoringProcess(string sqlCommand, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connectionStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sqlCommand;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(parameters);
cmd.ExecuteNonQuery();
return cmd.ExecuteNonQuery();
}
}
}
不需要DataTable 不需要SqlDataAdapter
返回值从parameters[0].Value就可以得到了