Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class Form3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
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("D:\Project VB.net\DB\BPlusDATA.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.DataGridView1.DataSource = dt
Me.DataGridView1.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 = "Server=IT-04S\SQLEXPRESS;UID=TestVB;PASSWORD=11111;database=TestVB;Max Pool Size=400;Connect Timeout=600;"
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()
End Sub
End Class
Tag : .NET, Ms SQL Server 2008, Excel (Excel.Application), VB.NET, VS 2010 (.NET 4.x)
Date :
2014-01-03 11:44:41
By :
Chunate
View :
1211
Reply :
5
No. 1
Guest
เอ่อ....
ที่ก็อปไปมันของ asp.net นะ
ใน win form มันไม่มี server.mappath กับ .databind() หรอก