使用mysql数据库在asp.net中注册数据失败

时间:2014-09-22 08:53:34

标签: mysql asp.net

我尝试使用asp.net和mysql构建注册码 代码是

protected void Button2_Click(object sender, EventArgs e)
{
    try
    {
        string MyConnection2 = "Server=xxx; Port=3306 ; Uid=xxx; pwd=xxx;Database=maintainance";
        string Query = "INSERT INTO `login`(`UName`, `UPasword`,`UserTypeID`,`Email`)VALUES('" + this.TextBox1.Text + "','" + this.TextBox2.Text + this.DropDownList1.SelectedValue +this.TextBox3.Text+ "');";
        MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
        MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
        MySqlDataReader MyReader2;
        MyConn2.Open();
        MyReader2 = MyCommand2.ExecuteReader();     
        Label3.Text = ("Your Data saved successfully");
        while (MyReader2.Read())
        {

        }
        MyConn2.Close();
    }
    catch
    {
           Label3.Text = ("Data saved failed");
    }
 }

但它显示我保存的数据失败...错误在哪里以及我如何更正?

1 个答案:

答案 0 :(得分:0)

您必须根据VALUES

之前的列传递值
  string Query = "INSERT INTO `login`(`UName`, `UPasword`,`UserTypeID`,`Email`) VALUES ('" + this.TextBox1.Text + "','" + this.TextBox2.Text+"', '"+this.DropDownList1.SelectedValue+"' ,'"+this.TextBox3.Text+ "');";