我在文本框中输入了一些值,并希望将这些值保存到我的 sql 数据库中。现在我只想在 MessageBox 中显示我的 SQL StoredProcedure 的返回值,以显示插入是否成功。
提交按钮代码:
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
if (txtType.Text.Length > 0 && txtNumber.Text.Length > 0 && txtLine1.Text.Length > 0)
{
string sOut = "";
DataManager.SaveAddendum(txtType.Text, txtNumber.Text, txtLine1.Text, txtLine2.Text, txtLine3.Text, txtLine4.Text, sOut);
MessageBox.Show(sOut);
}
}
提交按钮调用的数据管理类:
public static void SaveAddendum(string sType, string sNumber, string sLine1, string sLine2, string sLine3, string sLine4, string sOut)
{
// Create connection
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ToString());
// Create command
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_Insert_Addendum";
// Create and add input parameters
cmd.Parameters.AddWithValue("@Type", sType);
cmd.Parameters.AddWithValue("@Number", sNumber);
cmd.Parameters.AddWithValue("@Line1", sLine1);
cmd.Parameters.AddWithValue("@Line2", sLine2);
cmd.Parameters.AddWithValue("@Line3", sLine3);
cmd.Parameters.AddWithValue("@Line4", sLine4);
// Attach connection to command
cmd.Connection = con;
// Open connection
con.Open();
// Execute command
//cmd.ExecuteNonQuery();
sOut = cmd.ExecuteScalar().ToString();
// Close connection
con.Close();
// Dispose connection
con.Dispose();
}
存储过程:
ALTER PROCEDURE [dbo].[sp_Insert_Addendum]
-- Add the parameters for the stored procedure here
@Type varchar(3),
@Number varchar(4),
@Line1 varchar(60),
@Line2 varchar(60),
@Line3 varchar(60),
@Line4 varchar(60)
AS
BEGIN
SET NOCOUNT ON;
BEGIN
IF EXISTS(SELECT Addendum_Type, Addendum_Number FROM AddendumMst WHERE Addendum_Type = @Type AND Addendum_Number = @Number)
BEGIN
--PRINT N'The addendum already exists.
SELECT 'Already Exists'
END
ELSE
BEGIN
INSERT INTO AddendumMst (Addendum_Type, Addendum_Number, Line1, Line2, Line3, Line4) VALUES (@Type, @Number, @Line1, @Line2, @Line3, @Line4)
IF @@ERROR <> 0
BEGIN
--PRINT N'An error occurred inserting the addendum information.';
SELECT 'Error inserting'
END
ELSE
BEGIN
--PRINT N'The addendum has been inserted.';
SELECT 'Success'
END;
END
END
END
我的问题 - 消息框显示空白。