【发布时间】:2015-02-23 16:37:48
【问题描述】:
我正在使用 Microsoft Visual Studio 2013 并尝试使用 VB.NET 为我的帐户数据库制作注册表单。到目前为止,这是我的代码:
Private Sub btnRegistery_Click(sender As Object, e As EventArgs) Handles btnRegistery.Click
Dim usernme, passwrd As String
usernme = txtUsernm.Text
passwrd = txtpasswrd.Text
Dim myconnection As OleDbConnection
Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\hasan\Documents\Visual Studio 2012\Projects\hasan\Login_Info.accdb"
myconnection = New OleDbConnection(constring)
myconnection.Open()
Dim sqlQry As String
sqlQry = "INSERT INTO tbl_user(username, password) VALUES(usernme , passwrd)"
Dim cmd As New OleDbCommand(sqlQry, myconnection)
cmd.ExecuteNonQuery()
End Sub
代码编译得很好,但是当我尝试注册任何新信息时,我收到以下消息:
A first chance exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll
Additional information: Syntax error in INSERT INTO statement.
If there is a handler for this exception, the program may be safely continued.
这个问题的解决方案和原因是什么?
【问题讨论】:
-
我还建议您将所有数据库代码从用户界面代码中移出并放入单独的 DLL 中,以确保更好地分离关注点。
-
我还建议您不要对连接字符串进行硬编码,而是将它们存储在 app.config 文件中,这样您就可以“调整”MSAccess db 的位置而无需重新编译。
标签: database vb.net visual-studio ms-access syntax-error