簡體   English   中英

具有更改文本框名稱的功能C#

[英]Function with changing text box names C#

我正在構建的當前程序用於保存發票,並且我想將數據保存到數據庫中。 但是,我不想為每個可能的條目重復下面顯示的該代碼20次,我想創建一個函數,並在函數中更改文本框名稱。

所有文本框都以1到20的結尾命名。我想知道是否有一種方法可以使結尾的數字發生變化,並且與重復20次相比,是否值得這樣做。

if (txtProductID1.Text.Length > 0)
        {
            OleDbConnection oledbconnection1 = new OleDbConnection();
            oledbconnection1.ConnectionString = Con;
            OleDbCommand cmd;

            String strInsert = "";
            //Generate SQL Statement
            strInsert = "Insert into [InvoiceOrder] Values (";
            strInsert += "'1', ";
            strInsert += "'" + txtInvoiceNo.Text + "', ";
            strInsert += "'" + txtProductDescription1.Text + "', ";
            strInsert += "'" + txtOrderNo1.Text + "', ";
            strInsert += "'" + cboUnit1.Text + "', ";
            strInsert += "'" + txtAmount1.Text + "', ";
            strInsert += "'" + txtPrice1.Text + "', ";
            strInsert += "'" + txtSum1.Text + "', ";
            strInsert += "'" + txtDiscount1.Text + "' ";
            strInsert += ")";
            try
            {
                oledbconnection1.Open();
                cmd = new OleDbCommand();
                cmd.CommandText = strInsert;
                cmd.Connection = oledbconnection1;
                cmd.ExecuteNonQuery();
                //MessageBox.Show("Record saved");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.ToString());
            }
            finally
            {
                oledbconnection1.Close();
            }
        }

首先,參數化您的查詢。 除了安全性,當您忘記某個地方的單撇號時,正在建立的查詢也會使您不滿意。

至於遍歷控件,也許Controls.Find()將為您工作。 下面的代碼假定所有控件都有一個1到20的數字,並且每個數字在表單上出現一次且僅出現一次。 (在您的示例中, txtInvoiceNo沒有數字-我認為這是一個錯字。)

我也進行了其他一些更改,例如將您的finally塊替換為using塊,這將關閉並為您分配連接。

for (var i = 1; i <= 20; i++)
{    
    if (!String.IsNullOrEmpty(Controls.Find("txtProductID" + i, true).Single().Text))
    {
        using (var oledbconnection1 = new OleDbConnection())
        {
            oledbconnection1.ConnectionString = Con;
            oledbconnection1.Open();

            var insertStatement =
                "Insert into [InvoiceOrder] Values ('1', @InvoiceNo, @ProductDesc, @OrderNo, @Unit, @Amount, @Price, @Sum, @Discount)";

            try
            {
                using (var cmd = new OleDbCommand(insertStatement, oledbconnection1))
                {
                    cmd.Parameters.AddWithValue("@InvoiceNo", Controls.Find("txtInvoiceNo" + i, true).Single().Text);
                    ...
                    ...
                    cmd.Parameters.AddWithValue("@Discount", Controls.Find("txtDiscount" + i, true).Single().Text);
                    cmd.ExecuteNonQuery();
                    //MessageBox.Show("Record saved");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.ToString());
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM