Sql &引用;操作必须是可更新的查询;Net OleDB for Excel
我一直试图找到解决这个问题的办法,但没有成功: 我正在使用VB.NET,我需要从Excel文件中读取和更新记录。为此,我使用OleDB API。它可以很好地进行所有读取,但无法写入文件(更新或插入查询) 以下是我所拥有的: 我的连接字符串:Sql &引用;操作必须是可更新的查询;Net OleDB for Excel,sql,excel,vb.net,oledb,oledbexception,Sql,Excel,Vb.net,Oledb,Oledbexception,我一直试图找到解决这个问题的办法,但没有成功: 我正在使用VB.NET,我需要从Excel文件中读取和更新记录。为此,我使用OleDB API。它可以很好地进行所有读取,但无法写入文件(更新或插入查询) 以下是我所拥有的: 我的连接字符串: Public connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\...\Resources\attempt.xls;Extended Propertie
Public connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\...\Resources\attempt.xls;Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'"
选择工作正常的查询:
Dim checkQuery = "SELECT * FROM [Sheet1$] WHERE [TravellerPN] = @T"
Try
Using connection As New OleDb.OleDbConnection(Form.connString)
Dim checkCommand As New OleDbCommand(checkQuery, connection)
checkCommand.Parameters.Add("@T", OleDbType.VarChar).Value = PN
connection.Open()
Dim reader As OleDbDataReader = checkCommand.ExecuteReader()
Dim path As String = ""
While reader.Read()
path = reader("Datapath").ToString
End While
reader.Close()
MsgBox(PN & " " & DP & " " & path,,)
如果输入的零件号记录不存在,且未返回任何内容,请插入新行
If path = "" Then
Dim addQuery = "INSERT INTO [Sheet1$] ([TravellerPN],[Datapath]) VALUES (@T, @D)"
Dim addCommand As New OleDbCommand(addQuery, connection)
addCommand.Parameters.Add("@T", OleDbType.VarChar).Value = PN
addCommand.Parameters.Add("@D", OleDbType.VarChar).Value = DP
Dim rows As Integer = addCommand.ExecuteNonQuery()
这就是它返回错误的地方
MsgBox(rows & " row added!",, "") 'Never diplayed
以及其他不起作用的查询:
Else 'If does exist, confirm replacement'
Dim replaceResponse = MsgBox("A path already exists for " & PN & "." & vbNewLine & "Do you want to replace " & path & " with " & DP & "?", MsgBoxStyle.YesNo, "Overwrite previous datapath?")
Dim replaceQuery = "UPDATE [Sheet1$] SET [Datapath] = @D WHERE [TravellerPN]=@T"
Dim replaceCommand As New OleDbCommand(replaceQuery, connection)
replaceCommand.Parameters.Add("@D", OleDbType.VarChar).Value = DP
replaceCommand.Parameters.Add("@T", OleDbType.VarChar).Value = PN
Dim rows As Integer = replaceCommand.ExecuteNonQuery()
MsgBox(rows & " row updated!",, "")
End If
connection.Close()
End Using
我试图解决这个问题:它可能是由权限引起的,所以我甚至授权来宾帐户修改我的文件夹
如果它是连接中的只读模式:我尝试添加mode=ReadWrite,但此后我的连接字符串无法工作。(该选项似乎仅适用于ADO连接?)
我尝试以管理员身份运行该应用程序
最后,我把这个贴在这里,希望能得到一些帮助。
抱歉,或者是这篇长文章,我试图给出可能存在问题的所有元素
谢谢。另一篇标记为答案的SO帖子说明要从连接字符串中删除IMEX部分。天哪,为什么这么容易。非常感谢,对不起,我以为我看起来很好,但显然没有读那篇文章。再次感谢!