Create PROCEDURE [dbo].[Do]
@a int,
@b int
AS
BEGIN
return @a + @b
END测试代码:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer"].ToString()))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "Do";
cmd.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(cmd);
cmd.Parameters["@a"].Value = 2;
cmd.Parameters["@b"].Value = 3;
cmd.ExecuteNonQuery();
Console.WriteLine(cmd.Parameters["@RETURN_VALUE"].Value);
}
conn.Close();
}
Console.ReadKey();
}
}
}说明:SqlCommandBuilder.DeriveParameters读取存储过程参数填充到SqlCommand.Parameters集合,集合第一项参数名总是@RETURN_VALUE!
评论(0)
暂无评论。