2017-08-15 22 views
-1

我有這樣一段代碼與試&陷阱:獲取VB的一個錯誤:缺少分號(;)在SQL語句的結束

Try 
    comando = New OleDbCommand("INSERT INTO login VALUES (Usuario, Password, Cuenta)" & Chr(13) & 
     "VALUES(UsuarioText, PasswordText, TipoText)", conexion) 
    comando.Parameters.AddWithValue("@Usuario", UsuarioText.Text) 
    comando.Parameters.AddWithValue("@Password", PasswordText.Text) 
    comando.Parameters.AddWithValue("@Cuenta", TipoText.Text) 

    comando.ExecuteNonQuery() 
    MsgBox("El registro ha sido guardado exitosamente", MsgBoxStyle.Information, "Informacion") 


Catch ex As Exception 
    MsgBox("El registro no se pudo guardar", MsgBoxStyle.Critical, "Informacion") 
    MsgBox(ex.Message) 
End Try 

我將嘗試把一些;在修正錯誤報表的結尾,但仍然無法正常工作。

它可能是什麼?

+0

胡亂猜測,我從來沒有在VB中工作:'COMANDO =新的OleDbCommand(「INSERT INTO登錄值(Usuario,密碼,Cuenta)「&Chr(13)& 」VALUES(UsuarioText,PasswordText,TipoText);「,conexion)'你可以試試這個改變嗎? – Steephen

+1

將「VALUES(UsuarioText,PasswordText,TipoText)」更改爲「VALUES(@UsuarioText,@PasswordText,@TipoText)」,並使參數名稱與字符串中的內容匹配。 – Jeremy

+0

我同意@Steephen。另外,在發佈SQL相關問題時,最好包含您所定位的數據庫平臺。舉例來說,SQL Server並不傾向於關注這個';'字符,但是Oracle對此非常關注。不知道其他平臺,因爲這些是我最常用的兩個。 &Chr(13)&。這是什麼? – PSGuy

回答

0

試試這個辦法

' SQL COMMAND FOR INSERTING VALUES 
Dim objCommand As OleDbCommand = New OleDbCommand() 
objCommand.Connection = conexion 

objCommand.CommandText = "INSERT INTO login VALUES ([Usuario], [Password], [Cuenta])" & _ 
    "VALUES(@Usuario, @Password, @cuenta)" 

' CONTROLS BINDING 
objCommand.Parameters.AddWithValue("@Usuario", UsuarioText.Text) 
objCommand.Parameters.AddWithValue("@Password", PasswordText.Text) 
objCommand.Parameters.AddWithValue("@Cuenta", TipoText.Text) 

'OPEN CONNECTION 
conexion.Open() 
'EXECUTE CONNECTION 
objCommand.ExecuteNonQuery() 
MsgBox("El registro ha sido guardado exitosamente", MsgBoxStyle.Information, "Informacion") 

'CLOSE CONNECTION 
conexion.Close() 

我評論的代碼

希望它可以幫助每一部分

說起來很簡單的方法,我用我的項目

這是VALUES(UsuarioText, PasswordText, TipoText)不正確 更正一個VALUES(@Usuario, @Password, @cuenta),因爲我使用parameters.addWithValue來綁定控件

objCommand.Parameters.AddWithValue("@Usuario", UsuarioText.Text) 
objCommand.Parameters.AddWithValue("@Password", PasswordText.Text) 
objCommand.Parameters.AddWithValue("@Cuenta", TipoText.Text) 
0

嘿@NietzscheProgrammer

嘗試調整INSERT語句,所以它只有一行。刪除CHAR(13)並在變量中添加「@」符號。不要忘記將SEMICOLON放置在聲明的末尾。

Try 
    comando = New OleDbCommand("INSERT INTO login VALUES (Usuario, Password, Cuenta) VALUES(@UsuarioText, @PasswordText, @TipoText);", conexion) 
    comando.Parameters.AddWithValue("@Usuario", UsuarioText.Text) 
    comando.Parameters.AddWithValue("@Password", PasswordText.Text) 
    comando.Parameters.AddWithValue("@Cuenta", TipoText.Text) 

    comando.ExecuteNonQuery() 
    MsgBox("El registro ha sido guardado exitosamente", MsgBoxStyle.Information, "Informacion") 

Catch ex As Exception 
    MsgBox("El registro no se pudo guardar", MsgBoxStyle.Critical, "Informacion") 
    MsgBox(ex.Message) 
End Try 
+0

我還沒有得到與這種方法相同的錯誤前夕。 – NietzscheProgrammer

0

略去VALUES你的表後Login
它應該是:

comando = New OleDbCommand("INSERT INTO login (Usuario, Password, Cuenta) VALUES (UsuarioText, PasswordText, TipoText)", conexion) 

編輯:

另外:把 「@」

"VALUES (@UsuarioText, @PasswordText, @TipoText)", conexion) 

確保帶@的值是匹配的comando.Parameters
將其更改爲:

comando.Parameters.AddWithValue("@UsuarioText", UsuarioText.Text) 
comando.Parameters.AddWithValue("@PasswordText", PasswordText.Text) 
comando.Parameters.AddWithValue("@TipoText", TipoText.Text)