Mysql错误 - 数据检索

时间:2016-05-07 17:18:52

标签: mysql

以下代码有什么问题?我正在尝试从数据库中检索数据,并显示此错误。请提供解决方案。 错误:

您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在' wrap'附近使用。在第1行   描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

Line 19:         da.SelectCommand = cmd;
Line 20:         DataSet ds = new DataSet();
Line 21:         da.Fill(ds,"Recipe_Info");
Line 22:         DataRow dr = ds.Tables["Recipe_Info"].Rows[0];
Line 23:         String r_name, r_ing, r_desc, r_ins;

异常详细信息:MySql.Data.MySqlClient.MySqlException:SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在' wrap'附近使用。在第1行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;

public partial class Detailed_Recipe_View : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       String recipe_name=Session["Recipe_Name"].ToString();
        MySqlConnection con = new MySqlConnection("Server=localhost;Database=FreedomKitchen;Uid=root;Password=;");
        con.Open();
        MySqlCommand cmd = new MySqlCommand("select User_ID,Recipe_Name,All_Ingredients,Recipe_Description,Recipe_Instructions from Recipes where Recipe_Name="+recipe_name, con);
        MySqlDataAdapter da = new MySqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        da.Fill(ds,"Recipe_Info");
        DataRow dr = ds.Tables["Recipe_Info"].Rows[0];
        String r_name, r_ing, r_desc, r_ins;

        r_name = Convert.ToString(dr["Recipe_Name"]);
        r_ing = Convert.ToString(dr["All_Ingredients"]);
        r_desc = Convert.ToString(dr["Recipe_Description"]);
        r_ins = Convert.ToString(dr["Recipe_Instructions"]);

        txtRecipeName.Text = r_name;
        txtDescription.Text = r_desc;
        txtAllIngredients.Text = r_ing;
        txtInstructions.Text = r_ins;

    }
}

1 个答案:

答案 0 :(得分:0)

您应该在recipe_name条件WHERE周围放置单引号:

new MySqlCommand("select ... from Recipes where Recipe_Name = '" + recipe_name + "'", con);

更好的选择是使用参数化查询(Parameterized Query for MySQL with C#)。

相关问题