如果要获取 [@errormessage] 并将其打印在 ASPX 页面上,则需要使用ParameterDirection.Output
根据您的评论,我编写了一个完整的代码并在我的本地进行了测试,它按预期工作。请再试一次。
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Program.RunSPWithParameterOutput();
}
public static void RunSPWithParameterOutput()
{
string PredefinedConnectionString = "Data Source=localhost;Initial Catalog=Adventure;Integrated Security=SSPI";
using (SqlConnection conn = new SqlConnection(PredefinedConnectionString))
using (SqlCommand cmd = new SqlCommand("dbo.usp_ItemMasterIns", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
// set up the parameters
cmd.Parameters.Add("@ItemsCode", SqlDbType.VarChar, 30);
cmd.Parameters.Add("@ItemsDesc", SqlDbType.VarChar, 30);
cmd.Parameters.Add("@ErrorMessage", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output;
// set parameter values
cmd.Parameters["@ItemsCode"].Value = "test";
cmd.Parameters["@ItemsDesc"].Value = "test";
// open connection and execute stored procedure
conn.Open();
cmd.ExecuteNonQuery();
// read output value from @ErrorMessage
string ErrorMessage = cmd.Parameters["@ErrorMessage"].Value.ToString();
Console.WriteLine(ErrorMessage);
conn.Close();
}
}
}
}
测试部署在我本地的存储过程。
create procedure usp_ItemMasterIns (@ItemsCode varchar(100),
@ItemsDesc varchar(30),
@ErrorMessage varchar(200) output
)
as
begin
set @ErrorMessage='test error message'
end