我有一个简单的SQL Server存储过程:初始化存储过程的输出参数
ALTER PROCEDURE GetRowCount
(
@count int=0 OUTPUT
)
AS
Select * from Emp where age>30;
SET @[email protected][email protected]@ROWCOUNT;
RETURN
我试图初始化并在下面的C#代码访问的输出参数:
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=answers;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "GetRowCount";
cmd.Parameters.Add(new SqlParameter("@count", SqlDbType.Int));
cmd.Parameters["@count"].Direction = ParameterDirection.Output;
con.Open();
cmd.Parameters["@count"].Value=5;
cmd.ExecuteNonQuery();
int ans = (int)(cmd.Parameters["@count"].Value);
Console.WriteLine(ans);
但在运行代码,第InvalidCastException
被引发到代码的第二行(我调试并检查没有任何内容在Value属性中返回)。
如何正确地初始化代码中的输出参数?提前致谢!
可能重复(http://stackoverflow.com/questions/12174399/accessing-sql-server-stored-procedure-output -in-c-sharp) – podiluska
我是一个小时前问过这个问题的人,但这是一个完全不同的问题 – ankit0311