我在Visual Studio中将数据从excel文件导入datagridview 提示错误为
无法更新数据库或对象是只读的
当我执行程序时。到目前为止我已经完成的步骤
Public Class ExcelData
Private Sub ExcelData_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim path As String = "& TextBox2.text &"
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
dataSet = New System.Data.DataSet
MyCommand.Fill(dataSet)
DataGridView1.DataSource = dataSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End Sub
End Class
答案 0 :(得分:0)
我可能对此不正确,但下面的行看起来很奇怪......
Dim path As String = "& TextBox2.text &"
这会创建一个名为path
的字符串,其值为:& TextBox2.text &
...
然后在path
中使用此OleDbConnection
字符串:
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
在上面的一行中,您尝试连接DataSource
名为"& TextBox2.text& ... ???
我猜测代码表格TextBox2.Text
上的文本框包含您要打开的Excel文件的完整路径。如果是这种情况,那么只需更改变量字符串path
,如下所示:
Dim path As String = TextBox2.Text
只是一个想法。