我使用以下代码在给定用户输入的情况下运行存储过程。如果输入有效并且不会有重复记录,则存储过程会更新某些表 如果输入无效或该记录已存在,则存储过程返回分配给变量消息的OUT参数的值。 如何在屏幕上显示消息的值? Messabebox将是理想的,但找不到应该从哪里调用它。
partial void MyStorProc_Inserting(MyStorProcOperation entity)
{
using (SqlConnection connection = new SqlConnection())
{
string connMyDB = this.DataWorkspace.MyDB.Details.Name;
connection.ConnectionString = ConfigurationManager.ConnectionStrings[connMyDB].ConnectionString;
string proc = "[MyDB].[dbo].[szMyStorProc]";
using (SqlCommand command = new SqlCommand(proc, connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@Uname", entity.Uname));
command.Parameters.Add(new SqlParameter("@BU", entity.BU));
SqlParameter output = new SqlParameter();
output.ParameterName = "@ResultDescription";
output.SqlDbType = System.Data.SqlDbType.VarChar;
output.Direction = ParameterDirection.Output;
output.Size = 256;
command.Parameters.Add(output);
connection.Open();
command.ExecuteNonQuery();
string message = output.Value.ToString();
}
}
this.Details.DiscardChanges();
}
存储过程有点像这样:
CREATE PROCEDURE [dbo].[szMyStorProc]
-- Add the parameters for the stored procedure here
@Uname varchar(50) = '',
@BU varchar(50) = '',
@ResultDescription varchar(256)= '' OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @EmployeeID INT
DECLARE @BUID INT
SELECT @EmployeeID = ID FROM tEmployee WHERE Uname = @Uname
IF @EmployeeID IS NULL
BEGIN
SELECT @ResultDescription = 'User name not found'
PRINT @ResultDescription
END
ELSE
...
...
END
GO