在一列中添加多个值

时间:2014-02-10 15:02:42

标签: c# mysql

我的问题是,当我点击保存按钮时,只有txtEmail1的值会保存在email列中。

MySqlCommand cmd;
cmd.CommandText = "INSERT INTO email(Id,email)VALUES( @Id,@email)";
cmd.Parameters.AddWithValue("@Id", int.Parse(txtId.Text));
cmd.Parameters.AddWithValue("@email", txtEmail1.Text + "" + txtEmail2.Text);
cmd.ExecuteNonQuery();

1 个答案:

答案 0 :(得分:0)

试试这个

string[] email = new string[2];
email[0] = txtEmail1.Text;
email[1] = txtEmail2.Text;

   for (int i = 0; i < 2; i += 1)
    {
      MySqlCommand cmd;
      cmd.CommandText = "INSERT INTO email(Id,email)VALUES( @Id,@email)";
      cmd.Parameters.AddWithValue("@Id", int.Parse(txtId.Text));
      cmd.Parameters.AddWithValue("@email",email[i]);
      cmd.ExecuteNonQuery();   
    }