使用 oled 或 odbc 连接在 Excel 文件中插入、更新、删除数据

发布于 2024-07-17 22:32:57 字数 889 浏览 2 评论 0原文

我已经尝试了所有方法,但无法插入、更新和删除 Excel 文件。 我能够连接 Excel 工作表。 连接似乎也已打开。 这是我的连接字符串:

 Dim sConnectionString As String
        Const kunal = "C:\"
        sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & kunal & _
            "login.xls;Extended Properties=Excel 8.0;"
        Dim con As New OleDbConnection(sConnectionString)
        con.Open()
        'MsgBox("hi")

消息框来了。 所以连接没问题,但现在我只想将详细信息添加到 Excel 工作表中,但出现了错误。 这是我的插入代码:

  Dim cmd As OleDbCommand
        cmd = New OleDbCommand("insert into [Sheet1$] (FirstName,LastName) values('admin','kunal')", con)
        cmd.ExecuteNonQuery()
        con.Close()

这是我在页面加载时遇到的错误。

Microsoft Jet 数据库引擎找不到对象“Sheet1$”。 确保该对象存在并且拼写其名称和路径 正确命名。 我已经为他提供了正确的道路。 拼音也是 正确。

I have tried everything but I'm not able to insert, update and delete the excel file. I'm able to connect the excel sheet. The connection also seems to be opened. Here's my connection string:

 Dim sConnectionString As String
        Const kunal = "C:\"
        sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & kunal & _
            "login.xls;Extended Properties=Excel 8.0;"
        Dim con As New OleDbConnection(sConnectionString)
        con.Open()
        'MsgBox("hi")

The msgbox comes. So the connection is alright but now I just wanna add to details into the excel sheet but there comes the error.
Here's my code for inserting:

  Dim cmd As OleDbCommand
        cmd = New OleDbCommand("insert into [Sheet1$] (FirstName,LastName) values('admin','kunal')", con)
        cmd.ExecuteNonQuery()
        con.Close()

This is the error which I get on page load.

The Microsoft Jet database engine could not find the object 'Sheet1$'.
Make sure the object exists and that you spell its name and the path
name correctly. I have provided him the correct path. Spelling is also
correct.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葮薆情 2024-07-24 22:32:57

也许看看 Linq to Excel

Maybe look at Linq to Excel

酒浓于脸红 2024-07-24 22:32:57

Sheet1(不带 $)必须是您尝试写入的工作表名称,它还需要您在第一行中写入的列的列标题。 另请确保您没有在 Excel 中打开电子表格,否则会锁定它。 我尝试了你的语法,效果很好。

Sheet1 (without the $) has to be the Worksheet name that you are trying to write to, which also needs column headings for the columns you are writing in the first row. Also make sure you don't have the spreadsheet open in Excel, that would lock it. I tried your syntax and it works fine.

妄司 2024-07-24 22:32:57

尽管看起来并非如此,但可能仍然是连接问题。

http://support.microsoft.com/kb/316809

如果您看到的是这样的bug,这是一个误导性的异常。

It could still be an issue with the connection, even though it doesn't look like it.

http://support.microsoft.com/kb/316809

If what you are seeing is this bug, it is a misleading exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文