使用关系删除级联将数据插入多个表时出错

时间:2014-02-06 17:46:10

标签: c# mysql

当我尝试输入值并单击“提交”按钮时,它会在异常中显示错误

  

您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在第1行“paul@yahoo.com”)(我输入的电子邮件)附近使用正确的语法

int MobileNo;
string Mobile = txtMobile.Text;
int.TryParse(Mobile, out MobileNo);
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd;
connection.Open();
try 
{
    cmd = connection.CreateCommand();
    cmd.CommandText = "INSERT INTO phonebook(Id,Name,MobileNo)VALUES(@Id,@Name,@MobileNo)";
    cmd.Parameters.AddWithValue("@Id", int.Parse(txtId.Text));
    cmd.Parameters.AddWithValue("@Name", txtName.Text);
    cmd.Parameters.AddWithValue("@MobileNo", Mobile);
    cmd.ExecuteNonQuery();
    cmd.Parameters.Clear();
    cmd.CommandText = "INSERT INTO email(Id,email)VALUES( ,@email)";
    cmd.Parameters.AddWithValue("@Id", int.Parse(txtId.Text));
    cmd.Parameters.AddWithValue("@email", txtEmail1.Text);
    cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    if (connection.State == ConnectionState.Open)
    {
        connection.Close();
        LoadData();
    }
}

the image is my table

1 个答案:

答案 0 :(得分:1)

更改

  

“插入电子邮件(Id,电子邮件)VALUES(,@ email)”;

  

“插入电子邮件(Id,电子邮件)VALUES(@Id,@ email)”;

您指定将数据插入两列,但仅提供单个值。