2017-08-15 51 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)