2012-12-02 118 views
-2

我已經試過這幾個小時,但我想不出它我們有什麼錯我的sql命令有什麼問題?

var cmd = new SqlCommand(); 
cmd.Parameters.Clear(); 
cmd.Connection = mySqlConnection; 
cmd.CommandType = CommandType.Text; 
cmd.CommandText = 
    "select Calle.Id_Calle FROM" + 
     "TominPredial.Calle as Calle,"+ 
     "TominPredial.Colonia as Col," + 
     "TominPredial.Poblacion as Pob" + 
    "WHERE Calle.Nombre = @NombreCalle" + 
      "and Col.Id_Colonia = @id_col and Pob.Id_Poblacion = @id_pob"; 
cmd.Parameters.Add("@NombreCalle", nombreCalle); 
cmd.Parameters.Add("@id_col", id_col); 
cmd.Parameters.Add("@id_pob", id_pob); 
cmd.ExecuteScalar() 

它說近「大道」不正確的語法

+0

您正在從3個表中選擇一個字段。你爲什麼不加入桌子? – polin

+0

@polin我不需要加入他們,我只需要Id,這就是爲什麼我使用ExecuteScalar – poz2k4444

+2

這是一個隱式的內部聯接。 – melpomene

回答

10

有行之間沒有空格:

"select Calle.Id_Calle FROM" + 
    "TominPredial.Calle as Calle,"+ 
    "TominPredial.Colonia as Col," + 
    "TominPredial.Poblacion as Pob" + 
"WHERE Calle.Nombre = @NombreCalle" + 
     "and Col.Id_Colonia = @id_col and Pob.Id_Poblacion = @id_pob"; 

這將使查詢... FROMTominpredial.calle ... pobWHERE ..這將產生一個語法錯誤。

您必須在行末尾的關鍵字之間加空格。就像這樣:

... 
"select Calle.Id_Calle FROM" + 
" TominPredial.Calle as Calle,"+ 
" TominPredial.Colonia as Col," + 
" TominPredial.Poblacion as Pob" + 
" WHERE Calle.Nombre = @NombreCalle" + 
" and Col.Id_Colonia = @id_col and Pob.Id_Poblacion = @id_pob"; 
/\ 
put a Spaces here 
+0

你是對的,謝謝! – poz2k4444

+1

這個錯誤是在另外兩個地方。只需調試它,看到沒有換行符的文本值,並查看您忘記了空間的位置。 – SimpleVar