C#使用DataBindings在编辑输入后更新数据库

时间:2018-01-22 11:22:43

标签: c# asp.net wcf

我正在使用WCF服务和Windows窗体。 我的问题是如何在编辑文本字段后编写“更新”按钮。 在我的Windows窗体中,我有这段代码:

    private void btnUpdate_Click(object sender, EventArgs e)
    {
ServiceReference1.StockCardServicesClient sList = new ServiceReference1.StockCardServicesClient();
                ServiceReference1.StockList stockDetails = new ServiceReference1.StockList();
                StockCardList.EndEdit();

                sList.UpdateStockCard(stockDetails)
    }

在编辑文本框内容后,我想使用更新按钮进行更新。

public void UpdateStockCard(StockList stockDetails)
{
    con.Open();
    SqlCommand cmd = new SqlCommand("UPDATE StockCard SET PartNumber=@partNumber WHERE IDNumber='1731-000246'", con);
    cmd.Parameters.AddWithValue("@categoryName", stockDetails.CategoryName);
    cmd.Parameters.AddWithValue("@mainGroupName", stockDetails.MainGroupName);
    cmd.Parameters.AddWithValue("@subGroupName", stockDetails.SubGroupName);
    cmd.Parameters.AddWithValue("@idNumber", stockDetails.IDNumber);
    cmd.Parameters.AddWithValue("@partNumber", stockDetails.PartNumber);
    cmd.ExecuteNonQuery();
    con.Close();
}

以下是我的WCF服务DAL的代码

onRefresh() {
  this.router.routeReuseStrategy.shouldReuseRoute = function(){return false;};

  let currentUrl = this.router.url + '?';

  this.router.navigateByUrl(currentUrl)
    .then(() => {
      this.router.navigated = false;
      this.router.navigate([this.router.url]);
    });
  }

在我的BLL和耳鼻喉科工作正常。

谢谢

2 个答案:

答案 0 :(得分:0)

实际上,有22个字段或文本框要更新。我只展示了样本的代码。但如果我将在下面编写此代码,它将起作用。

private void btnUpdate_Click(object sender, EventArgs e)
    {
                ServiceReference1.StockList stockDetails = new ServiceReference1.StockList();

            stockDetails.CategoryName = txtID.Text;
            stockDetails.MainGroupName = txtOPCOID.Text;
            stockDetails.SubGroupName = txtSubGroupID.Text;
            stockDetails.IDNumber = txtIDNumber.Text;
            stockDetails.PartNumber = txtPartNumber.Text;

                sList.UpdateStockCard(stockDetails);
}

我知道有更好的方法来更新数据绑定中的记录,但我不知道如何编码。

答案 1 :(得分:0)

我已经明白了。这是我的代码。

public partial class frmStockList : Form
{
    ServiceReference1.StockCardServicesClient stockList = new ServiceReference1.StockCardServicesClient();
    ServiceReference1.StockList stockDetails = new ServiceReference1.StockList();

    public frmStockList(frmMainForm mainForm)
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ShowStockListDetails();
    }

    private void ShowStockListDetails()
    {
        stockDetails.SearchIDNumber = frmMainForm.stockCardID.ToString();

        var StockList = stockList.GetParticularCard(stockDetails);

        stockDetails = StockList[0];

        BindingSource StockCardList = new BindingSource();

        StockCardList.ResetBindings(false);

        StockCardList.DataSource = stockDetails;

        txtID.DataBindings.Add(new Binding("Text", StockCardList, "CategoryName", true));
        txtOPCOID.DataBindings.Add(new Binding("Text", StockCardList, "MainGroupName", true));
        txtSubGroupID.DataBindings.Add(new Binding("Text", StockCardList, "SubGroupName", true));
        txtIDNumber.DataBindings.Add(new Binding("Text", StockCardList, "IDNumber", true));
        txtPartNumber.DataBindings.Add(new Binding("Text", StockCardList, "PartNumber", true));
        txtDescription.DataBindings.Add(new Binding("Text", StockCardList, "Description", true));
        txtMinimum.DataBindings.Add(new Binding("Text", StockCardList, "Minimum", true));
        txtMaximum.DataBindings.Add(new Binding("Text", StockCardList, "Maximum", true));
        cmbUOMPO.DataBindings.Add(new Binding("Text", StockCardList, "UOMPO", true));
        cmbUOMSTOCK.DataBindings.Add(new Binding("Text", StockCardList, "UOMStock", true));
        cmbUOMPERISSUE.DataBindings.Add(new Binding("Text", StockCardList, "UOMIssue", true));
        txtUOMRatio.DataBindings.Add(new Binding("Text", StockCardList, "UOMRatio", true));
        txtWidth.DataBindings.Add(new Binding("Text", StockCardList, "Width", true));
        txtHeight.DataBindings.Add(new Binding("Text", StockCardList, "Height", true));
        txtLength.DataBindings.Add(new Binding("Text", StockCardList, "Length", true));
        txtNetWeight.DataBindings.Add(new Binding("Text", StockCardList, "NetWeight", true));
        txtWarningExpiration.DataBindings.Add(new Binding("Text", StockCardList, "WarningPeriod", true));
        txtShelfLife.DataBindings.Add(new Binding("Text", StockCardList, "ShelfLife", true));
        txtCostCode.DataBindings.Add(new Binding("Text", StockCardList, "CostCodePurchase", true));
        cmbIssueToJob.DataBindings.Add(new Binding("Text", StockCardList, "CostCodeIssueToJob", true));
        cmbIssueToWS.DataBindings.Add(new Binding("Text", StockCardList, "CostCodeIssueToWS", true));
        cmbIssueToOPCO.DataBindings.Add(new Binding("Text", StockCardList, "CostCodeIssueToOPCO", true));
        cmbIssueForDisposalSCD.DataBindings.Add(new Binding("Text", StockCardList, "CostCodeIssueToDisposalSCD", true));
        cmbIssueForDisposalDMG.DataBindings.Add(new Binding("Text", StockCardList, "CostCodeIssueToDisposalDMG", true));
        cmbIssueForDisposalEXP.DataBindings.Add(new Binding("Text", StockCardList, "CostCodeIssueToDisposalEXP", true));
        cmbIssueForDisposalOBS.DataBindings.Add(new Binding("Text", StockCardList, "CostCodeIssueToDisposalOBS", true));
    }
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
        Dispose();
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        stockDetails.ID = frmMainForm.stockCardID.ToString();

        stockList.UpdateStockCard(stockDetails);

        MessageBox.Show("Update successfull!", "Update Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
        Close();
        Dispose();
    }
}