次のコードを使用して、ユーザーからの入力が与えられたストアド プロシージャを実行しています。入力が有効で、重複するレコードが存在しない場合、ストアド プロシージャはいくつかのテーブルを更新します。
入力が無効であるか、そのレコードが既に存在する場合、ストアド プロシージャは、変数 message に割り当てられた 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