问题是当我点击此代码所在的保存按钮时...它说已经定义了参数'@email'...我想要发生的是将这些值插入一列(电子邮件) )但不在同一行
string[] email = new string[2];
email[0] = txtEmail1.Text;
email[1] = txtEmail2.Text;
MySqlCommand cmd;
cmd.CommandText = "INSERT INTO email(Id,email)VALUES( @Id,@email)";
cmd.Parameters.AddWithValue("@Id", int.Parse(txtId.Text));
for (int i = 0; i < 2; i += 1)
{
cmd.Parameters.AddWithValue("@email", email[i]);
cmd.ExecuteNonQuery();
if (i==2)
break;
}
答案 0 :(得分:3)
清除循环每次迭代中的参数。
cmd.CommandText = "INSERT INTO email (Id,email) VALUES ( @Id, @email )";
for (int i = 0; i < 2; i++)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@Id", int.Parse(txtId.Text));
cmd.Parameters.AddWithValue("@email", email[i]);
cmd.ExecuteNonQuery();
}