2016-05-07 56 views
0

以下代碼有什麼問題?我試圖從數據庫中檢索數據,並顯示此錯誤。請提供解決方案。 錯誤:Mysql錯誤 - 數據檢索

您的SQL語法錯誤;檢查與你的MySQL服務器版本相對應的手冊,在第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服務器版本使用附近的「包裹」在行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; 

    } 
} 
+0

但它顯示第21行爲錯誤。 – user6302221

+0

看起來問題在這裏:Recipe_Name =「+ recipe_name ..檢查recipe_name變量的內容 – Marco

回答

0

你應該把recipe_name圍繞單引號的WHERE條件正確的語法手冊:

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

更好的選擇是使用參數化查詢(Parameterized Query for MySQL with C#)。