我有一个带有OUTPUT参数的存储过程:
CREATE PROCEDURE [dbo].[myStoredProcedure]
(@myValue NVARCHAR(100) OUTPUT)
AS
BEGIN
SET @myValue = (SELECT myValue FROM myTable WHERE something = 1)
SElECT @myValue;
END
我的C#类:
public string getOutPut()
{
string shortFooter;
string sql = "myStoredProcedure";
string connStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection DBConn = new SqlConnection(connStr))
{
using (SqlCommand DBCmd = new SqlCommand(sql, DBConn))
{
try
{
DBConn.Open();
DBCmd.CommandType = CommandType.StoredProcedure;
SqlParameter newSqlParam = new SqlParameter();
newSqlParam.ParameterName = "@myValue";
newSqlParam.SqlDbType = SqlDbType.NVarChar;
newSqlParam.Direction = ParameterDirection.Output;
DBCmd.Parameters.Add(newSqlParam);
DBCmd.ExecuteNonQuery();
shortFooter = DBCmd.Parameters["@myValue"].Value.ToString();
}
catch (Exception ex)
{
string blah = ex.ToString();
shortFooter = "";
}
}
}
return shortFooter;
}
这是我得到的例外:
System.InvalidOperationException:String [0]:Size属性的大小无效 .
当我在数据库中创建新记录时,我在't understand is, I have used this code before, except that in the stored procedure I'获取输出值't understand is, I have used this code before, except that in the stored procedure I',因此类中的参数类型为INT . 但在概念上它应该是相同的,但我想我知道为什么 .
我错过了什么?
谢谢!!
1 回答
您需要指定保持输出值所需的大小 . 从MSDN:
在代码中设置输出参数的Size以匹配存储过程中的内容:
默认值为0,这是导致异常的原因 .