0

ここに状況があります。私はそれを開始する考えさえ持っていないので、コードを書いていません!. 10 個の textboxes と 1 つのボタンがあるので、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' を空にしても問題ありません。

4

4 に答える 4

1

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
}
于 2012-07-28T07:16:42.473 に答える
1

それだけの価値があるため、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を使用して、接続が破棄される (閉じられる) ことを確認したことに注意してください。

于 2012-07-28T07:35:52.343 に答える
1

それはついに働いた!!! @ヨギありがとう!

機能したコードは

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();
          } 
    } 
于 2012-07-28T10:51:34.977 に答える
0

テキストボックスコントロールをリストに追加します。次に、リストをループします。

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インジェクションを受け入れることができます。

于 2012-07-28T07:16:21.950 に答える