簡體   English   中英

如何在C#中循環遍歷textBox(i)?

[英]How to loop through textBox(i) in c#?

我這里有個情況。 我還沒有編寫代碼,因為我什至沒有啟動它的想法! 我有10個文本框和一個按鈕,所以當我只輸入3個文本框時,我將僅使用3個作為解析到這些文本框的值進入數據庫。我打算在for循環中編寫查詢並執行它,以便只有具有價值的文本框才能進入數據庫。

for(int i=0;i<9;i++)
{
 string sql = "Insert Into exprmnt(docid,itemid,doctitle,itemcontent)values("+int.Parse(label6.Text)+","+i+",'"+label5.Text+"','"+textBox[i].Text+"')";

}
   OleDbCommand cmd = new OleDbCommand(sql,con);
  con.Open();

       cmd.ExecuteNonQuery();
  con.Close();

這是我想發生的事情,如果我在保存所有文本框(包括沒有鍵入任何文本框)時對某些itemid'i'保留空的'itemcontent',也可以。

要遍歷textBox,可以創建要遍歷的textBox數組:

TextBox[] tbs = {textBox1, textBox2, textBox3}; //put all into this array
for(int i = 0; i<tbs.Length; i++)
{
    //use each textBox in here:
    string text = tbs[0].Text; //this is an example of how to get the Text property of each textBox from array
}

對於它的價值,您可以使用Linq查找填充的文本框。 您可以進行SQL注入。 使用參數代替字符串連接:

int docid = int.Parse(label6.Text);
String doctitle = label5.Text;
var filledTextBoxes = this.Controls
                             .OfType<TextBox>()
                             .Select((txt,i) => new { Textbox = txt, Index = i })
                             .Where(x => x.Textbox.Text.Length != 0);
if(filledTextBoxes.Any())
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        const String sql = "Insert Into exprmnt(docid,itemid,doctitle,itemcontent)values(@docid, @itemid, @doctitle, @itemcontent)";
        connection.Open();
        foreach(var txt in filledTextBoxes)
        {
            OledDbCommand cmd = new OledDbCommand(sql, connection);
            // Set the parameters.
            cmd.Parameters.Add(
                "@docid", OleDbType.Integer).Value = docid;
            cmd.Parameters.Add(
                "@doctitle", OleDbType.VarChar, 50).Value = doctitle;
            cmd.Parameters.Add(
                "@itemid", OleDbType.Integer).Value = txt.Index;
            cmd.Parameters.Add(
                "@itemcontent", OleDbType.VarChar, 100).Value = txt.Textbox.Text;
            try
            {
                int affectedRecords = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        } 
    } // The connection is automatically closed when the code exits the using block.
}

請注意,我已經使用了using-statement來確保連接被釋放(關閉)。

終於奏效了!!! @yogi謝謝!

起作用的代碼是

List<TextBox> textBoxList = new List<TextBox>();
          textBoxList.Add(new TextBox { Text = textBox1.Text });
          textBoxList.Add(new TextBox { Text = textBox2.Text });
          textBoxList.Add(new TextBox { Text = textBox3.Text });
          textBoxList.Add(new TextBox { Text = textBox4.Text });



          for (int n = 1; n<4; n++)
          {

              string sql = "Insert Into exprmnt (docid,itemid,doctitle,itemcontent) values(" + int.Parse(label6.Text) + "," + n + ",'" + label5.Text + "','" + textBoxList[n].Text+ "')";

              OleDbCommand cmd = new OleDbCommand(sql, connection);

              connection.Open();

              cmd.ExecuteNonQuery();
              connection.Close();
          } 
    } 

將您的文本框控件添加到列表中。然后在列表上循環。

List<TextBox> textBoxList = new List<TextBox>();

for (int i = 0; i < textBoxList.Count; i++) {
    // .. do your thing here, using textBoxList[i].Text for the value in the textbox
}

此外,正如Tim Schmelter所指出的那樣,當您串聯查詢時,您也可以進行SQL注入。

暫無
暫無

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

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