C# 如何使用一个按钮向表中插入值并更新另一个表中的值?

C# 如何使用一个按钮向表中插入值并更新另一个表中的值?,c#,winforms,insert,updates,sqlparameter,C#,Winforms,Insert,Updates,Sqlparameter,我有两个表,叫做payment和appointment,它们使用appointmentid相互关联。对于我的当前代码,它只在付款表中插入值。我想要实现的是,如果我在表单中输入包括AppointId在内的值,然后单击submit,我刚刚输入的AppointId也会将该约会表的记录aStatus更新为completed或waiting。对于我的aStatus combobox,我在item属性中填充waiting and completed。目前我的代码只能插入到付款表中。阿斯塔斯在另一张桌子上,那

我有两个表,叫做payment和appointment,它们使用appointmentid相互关联。对于我的当前代码,它只在付款表中插入值。我想要实现的是,如果我在表单中输入包括AppointId在内的值,然后单击submit,我刚刚输入的AppointId也会将该约会表的记录aStatus更新为completed或waiting。对于我的aStatus combobox,我在item属性中填充waiting and completed。目前我的代码只能插入到付款表中。阿斯塔斯在另一张桌子上,那是预约桌

string value = cbaStatus.SelectedItem == null ? "waiting" : cbaStatus.SelectedItem.ToString();
updateCmd.Parameters.AddWithValue("@cbaStatus", value);
这是我为aStatus插入的dropdownlist代码。我想让它更新预约表的状态。那么,如何将此代码与底部代码组合在一个按钮中?此代码将更新约会表中的aStatus,底部的代码将在付款表中插入值

string value = cbaStatus.SelectedItem == null ? "waiting" : cbaStatus.SelectedItem.ToString();
updateCmd.Parameters.AddWithValue("@cbaStatus", value);
我的表格

我的表格和关系

遵循steve代码时出错

private void btnSubmit_Click(object sender, EventArgs e)
{
    int result = AddPaymentRecord();
    if (result > 0)
    {
        MessageBox.Show("Insert Successful");
        txtamount.Clear();
        txtamountPaid.Clear();
        txtappointmentID.Clear();
        txtamount.Focus();
    }
    else
    {
        MessageBox.Show("Insert Fail");
        txtamount.Clear();
        txtamountPaid.Clear();
        txtappointmentID.Clear();
        txtamount.Focus();
    }
}

private int AddPaymentRecord()
{
    int result = 0;

    string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;

    SqlConnection myConnect = new SqlConnection(strConnectionString);

    String strCommandText = "INSERT PAYMENT(amount, amountPaid, paymentDate, paymentType, appointmentID) "
        + " VALUES (@Newamount, @NewamountPaid,@NewpaymentDate, @NewpaymentType, @NewappointmentID)";

    SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect);
    updateCmd.Parameters.AddWithValue("@Newamount", txtamount.Text);
    updateCmd.Parameters.AddWithValue("@NewamountPaid", txtamountPaid.Text);
    updateCmd.Parameters.AddWithValue("@NewpaymentDate", dtppaymentDate.Value);
    if (rbCash.Checked)
        updateCmd.Parameters.AddWithValue("@NewpaymentType", "Cash");
    else
        updateCmd.Parameters.AddWithValue("@NewpaymentType", "Credit Card");
    updateCmd.Parameters.AddWithValue("@NewappointmentID", txtappointmentID.Text);

    myConnect.Open();

    result = updateCmd.ExecuteNonQuery();

    myConnect.Close();
    return result;
}


您有两个选项,将参数传递给存储过程,该存储过程在付款表中插入记录并更新任命表,或者从代码中执行两个命令

无论哪种方式,您都需要提供一个事务,以避免存储支付记录(在出现错误的情况下)和无法更新相应的预约记录

让我们试试代码版本(请测试它,因为我在这里是动态编写的)


在从XML成功地将数据插入表A之后,我想在表B中插入两个字段,即“for_dt”和另一个是“load indication”。

@Jordjmax在某个时候,您必须拥有所有权并理解您为学校项目提交的代码。opp我认为约会有拼写错误。好吧,哈哈。@Keith Payne