Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何从VisualStudio填充多访问表_Vb.net_Oledb - Fatal编程技术网

Vb.net 如何从VisualStudio填充多访问表

Vb.net 如何从VisualStudio填充多访问表,vb.net,oledb,Vb.net,Oledb,提前道歉-我是visual studio(以及一般编程)的新手 我正试图使用以下代码填充两个访问表Lown和property,但我不断得到 “SQL语句结尾缺少分号(;)。” 有什么建议吗?代码如下: Dim aConnection As OleDbConnection Dim aCommand As OleDbCommand Dim aConnectionString, aQuery As String Dim Username As String = txtUsername.Text Dim

提前道歉-我是visual studio(以及一般编程)的新手

我正试图使用以下代码填充两个访问表Lown和property,但我不断得到

“SQL语句结尾缺少分号(;)。”

有什么建议吗?代码如下:

Dim aConnection As OleDbConnection
Dim aCommand As OleDbCommand
Dim aConnectionString, aQuery As String
Dim Username As String = txtUsername.Text
Dim Pword As String = TextBox2.Text
Dim EmailDetails As String = TextBox9.Text
Dim Question As String = DropDownList2.Text
Dim Answer As String = TextBox4.Text

aConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = " _
& Server.MapPath("AppData/RMT.accdb")
aConnection = New OleDbConnection(aConnectionString)
aConnection.Open()
aQuery = "Insert Into Landlord (Username, email, Security_Question, Security_Answer, Pword, Status) Values ('" & Username & "','" & EmailDetails & "','" & Question & "','" & Answer & "', '" & Pword & "','pending')Into Property Values (26,46,'marysway','MarysRd','Marysville','Cork North')"
aCommand = New OleDbCommand(aQuery, aConnection)
aCommand.ExecuteNonQuery()

aConnection.Close()

我认为Microsoft Access的ADO.NET提供程序不支持在同一查询字符串中使用多个insert语句。但是,为了检查,您可以尝试编写

aQuery = "Insert Into Landlord (Username, email, Security_Question, Security_Answer, " & _ 
         "Pword, Status) Values ('" & Username & "','" & EmailDetails & "','" & _
         Question & "','" & Answer & "', '" & Pword & "','pending');" & _
         "Insert Into Property Values (26,46,'marysway','MarysRd','Marysville','Cork North')"
注意第一个INSERT语句和第二个INSERT语句之间的分号(顺便说一下,原始代码中缺少第二个INSERT关键字)

尽管如此,我建议在构建要传递给数据库引擎的查询文本时始终使用OleDbCommand的参数集合。
这将避免文本解析的问题(输入文本中的一个引号会破坏一切),但也不会将代码公开给其他人


属性值中的Pword&“,”pending')看起来很奇怪,但“我不是访问专家。作为一般提示:在OleDBCommand中使用OLEDB参数。这将使您的代码更易于阅读,更不容易出错(请尝试插入日期;))谢谢您的帮助-这给了我一条消息说”INSERT INTO语句中的语法错误“-回到绘图板上,我想你是指双插入语句,还是指第二个示例中的单独语句?在第二种情况下,是第一次插入还是第二次插入失败?实际上,史蒂夫-第二个选项工作得很好,非常感谢您的帮助!正如我所想。Microsoft Access的ADO.NET驱动程序无法处理同一查询中的多个语句。对于Microsoft.Jet.OleDb.4.0,我是肯定的,不确定每个ACE,但看起来是一样的。您应该使用单独的语句。
Using aConnection = New OleDbConnection(aConnectionString)
    aConnection.Open()

    aQuery = "Insert Into Landlord (Username, email, Security_Question, " + 
             "Security_Answer, Pword, Status) Values (?, ?, ?,?,?,'pending')"
    aCommand = New OleDbCommand(aQuery, aConnection)
    aCommand.Parameters.AddWithValue("@usr", Username)
    aCommand.Parameters.AddWithValue("@email", EmailDetails)
    aCommand.Parameters.AddWithValue("@qst", Question)
    aCommand.Parameters.AddWithValue("@ans", Answer)
    aCommand.Parameters.AddWithValue("@pwd", Pword)
    aCommand.ExecuteNonQuery()

    aQuery = "Insert Into Property Values (26,46,'marysway','MarysRd','Marysville','Cork North')"
    aCommand = New OleDbCommand(aQuery, aConnection)
    aCommand.ExecuteNonQuery()

End Using