如何循环使用c#中的文本框(i)?

如何循环使用c#中的文本框(i)?,c#,database,dynamic,textbox,C#,Database,Dynamic,Textbox,我这里有个情况。我还没有写代码,因为我甚至没有启动它的想法!。我有10个文本框和一个按钮,所以当我只输入3个文本框时,我将只使用3个文本框,因为我正在解析这些文本框的值进入数据库。我计划在for循环中编写一个查询并执行它,以便只有有值的文本框进入数据库 for(int i=0;i<9;i++) { string sql = "Insert Into exprmnt(docid,itemid,doctitle,itemcontent)values("+int.Parse(label6.Te

我这里有个情况。我还没有写代码,因为我甚至没有启动它的想法!。我有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();

for(int i=0;i将文本框控件添加到列表中..然后在列表上循环

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
}
List textBoxList=newlist();
对于(int i=0;i

此外,正如Tim Schmelter所指出的那样,当连接查询时,您可以进行SQL注入。

将文本框控件添加到列表中。。然后循环列表

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
}
List textBoxList=newlist();
对于(int i=0;i

此外,正如Tim Schmelter指出的那样,当连接这样的查询时,您可以进行SQL注入。

要循环使用文本框,您可以创建一个要循环使用的文本框数组:

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
}
TextBox[]tbs={textBox1,textBox2,textBox3};//将所有内容放入此数组中

对于(int i=0;i要循环浏览文本框,可以创建要循环浏览的文本框数组:

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
}
TextBox[]tbs={textBox1,textBox2,textBox3};//将所有内容放入此数组中

对于(int i=0;i,您可以使用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.
}
intdocid=int.Parse(label6.Text);
字符串doctitle=label5.Text;
var filledtextbox=this.Controls
第()类
.Select((txt,i)=>new{Textbox=txt,Index=i})
。其中(x=>x.Textbox.Text.Length!=0);
if(filledtextbox.Any())
{
使用(OLEDB连接=新OLEDB连接(connectionString))
{
const String sql=“插入exprmnt(docid、itemid、doctitle、itemcontent)值(@docid、@itemid、@doctitle、@itemcontent)”;
connection.Open();
foreach(填充文本框中的var txt)
{
OledDbCommand cmd=新的OledDbCommand(sql,连接);
//设置参数。
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;
尝试
{
int affectedRecords=cmd.ExecuteNonQuery();
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
} 
}//当代码退出using块时,连接将自动关闭。
}

请注意,我已经使用了来确保连接被释放(关闭)。

无论如何,您可以使用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.
}
intdocid=int.Parse(label6.Text);
字符串doctitle=label5.Text;
var filledtextbox=this.Controls
第()类
.Select((txt,i)=>new{Textbox=txt,Index=i})
。其中(x=>x.Textbox.Text.Length!=0);
if(filledtextbox.Any())
{
使用(OLEDB连接=新OLEDB连接(connectionString))
{
const String sql=“插入exprmnt(docid、itemid、doctitle、itemcontent)值(@docid、@itemid、@doctitle、@itemcontent)”;
connection.Open();
foreach(填充文本框中的var txt)
{
OledDbCommand cmd=新的OledDbCommand(sql,连接);
//设置参数。
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;
尝试
{
int affectedRecords=cmd.ExecuteNonQuery();
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
} 
}//当代码退出using块时,连接将自动关闭。
}
请注意,我已经使用了来确保连接被释放(关闭)。

它终于工作了!!! @瑜伽修行者,谢谢

有效的代码是

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 textBoxList=newlist();
添加(新文本框{Text=textBox1.Text});
添加(新文本框{Text=textBox2.Text});
添加(新文本框{Text=textBox3.Text});
添加(新文本框{Text=textBox4.Text});
对于(int n=1;n它终于起作用了!!!
@瑜伽修行者,谢谢

有效的代码是

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 textBoxList=newlist();
添加(新文本框{Text=textBox1.Text});
添加(新文本框{Text=textBox2.Text});
添加(新文本框{Text=textBox3.Text});
添加(新文本框{Text=textBox4.Text});

对于(int n=1;您可以进行SQL注入。请使用而不是字符串连接。只是不要使用文本框,因为它的伸缩性非常差,会让用户感到困惑。请使用DataGridView。您可以进行SQL注入。请使用而不是字符串连接。请不要使用文本框