简体   繁体   English

将记录插入sql db后返回一个值

[英]Return a value after inserting record into sql db

I'm entering some values into textboxes and want to save these values to my sql db. 我正在文本框中输入一些值,并将这些值保存到我的sql db中。 For now I just want to display a return value from my SQL StoredProcedure in a MessageBox to show whether the insert was a success. 现在,我只想在MessageBox中显示SQL StoredProcedure的返回值,以显示插入是否成功。

Submit button code: 提交按钮代码:

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);                
    }
}

Data management class called by the submit button: 提交按钮调用的数据管理类:

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();
}

Stored procedure: 存储过程:

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

My problem - the Messagebox displays a blank. 我的问题 - 消息框显示一个空白。

You're SaveAddendum mthod doesn't return a value. 你是SaveAddendum方法没有返回值。 sOut is local to the method. sOut对方法sOut是本地的。 Change your method to return a value (a string), and then you can display it in the MessageBox. 更改您的方法以返回值(字符串),然后可以在MessageBox中显示它。 Also, you can remove the sOut, as that isn't needed (and won't do what you're thinking it will unless you declare it as an out parameter): 此外,您可以删除sOut,因为这不是必需的(除非您将其声明为out参数,否则不会做您想做的事情):

public static string SaveAddendum(string sType, string sNumber, string sLine1, string sLine2, string sLine3, string sLine4)
{

    // Your code

    return sOut;
}

Then when you call the method, you can do something like this: 然后当你调用方法时,你可以这样做:

string sOut = DataManager.SaveAddendum(txtType.Text, txtNumber.Text, txtLine1.Text, txtLine2.Text, txtLine3.Text, txtLine4.Text);

MessageBox.Show(sOut); 

You should declare the sOut parameter as an output one: 您应该将sOut参数声明为输出参数:

public static void SaveAddendum(string sType, string sNumber, string sLine1, string sLine2, string sLine3, string sLine4, out string sOut)

and call it like this: 并称之为:

DataManager.SaveAddendum(txtType.Text, txtNumber.Text, txtLine1.Text, txtLine2.Text, txtLine3.Text, txtLine4.Text, out sOut);

Unrelated to the problem, I would also suggest to use using to handle the closing of the connection: 与此问题无关,我还建议使用using来处理连接的关闭:

using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ToString())) {

    // . . . code

}

where the } replaces }替换的位置

con.Close();
con.Dispose();

The code is shorter and it guarantees that the connection is properly closed even if there is an exception. 该代码较短,并且即使有异常,也可以确保正确关闭连接。

I think you should mark parameter sOut in SaveAddendum procedure with "ref". 我认为您应该在SaveAddendum过程中用“ ref”标记参数sOut。 Otherwise method doesnt know it should return value assigned to this parameter. 否则方法不知道它应该返回分配给此参数的值。 But much better design would be to return this parameter from SaveAddendum, instead of having it return void. 但更好的设计是从SaveAddendum返回此参数,而不是让它返回void。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM