Dim objConn As New OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source="&Server.MapPath("MyXls/MyExcelDB.xls")&";Extended Properties=Excel 8.0;"
objConn = New OleDbConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM [Sheet1$]"
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
'*** BindData To DataGrid ***'
Me.myDataGrid.DataSource = dt
Me.myDataGrid.DataBind()
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Dim connetionString As String
Dim connection As SqlConnection
Dim adapter As New SqlDataAdapter
Dim i As Integer
Dim sql As String
connetionString = "Data Source=ServerName;Initial Catalog=MyDatabase;User ID=UserName;Password=Password"
connection = New SqlConnection(connetionString)
connection.Open()
For i = 0 To dt.Rows.Count - 1
sql = "insert into product (Product_id,Product_name,Product_price) values('" & dt.Rows(i)(0) & "','" & dt.Rows(i)(1) & "','" & dt.Rows(i)(2) & "')"
adapter.InsertCommand = New SqlCommand(sql, connection)
adapter.InsertCommand.ExecuteNonQuery()
Next
connection.Close()
ขอผมมันรันไม่ได้อะครับ มันบอกว่า
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.