แต่ไม่รู้จะต้อง ดึงข้อมูลในแต่ละช่องมาไว้ในตัวแปรหรือ.textได้อย่างไร (เช่น lblID.text) เพื่อให้เซฟลงตารางในดาต้าเบสได้
ต้อง for วน ด้วยอะไร
โค้ด ที่ upload มา Code (VB.NET)
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpload.Click
If FileUpload1.HasFile Then
Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
Dim FilePath As String = Server.MapPath(FolderPath + FileName)
FileUpload1.SaveAs(FilePath)
Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
End If
End Sub
Private Sub Import_To_Grid(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String)
Dim conStr As String = ""
conn.Close()
conn.Open()
Select Case Extension
Case ".xls"
'Excel 97-03
conStr = ConfigurationManager.ConnectionStrings("Excel03ConString") _
.ConnectionString
Exit Select
Case ".xlsx"
'Excel 07
conStr = ConfigurationManager.ConnectionStrings("Excel07ConString") _
.ConnectionString
Exit Select
End Select
conStr = String.Format(conStr, FilePath, isHDR)
Dim connExcel As New OleDbConnection(conStr)
Dim cmdExcel As New OleDbCommand()
Dim oda As New OleDbDataAdapter()
Dim dt As New DataTable()
cmdExcel.Connection = connExcel
'Get the name of First Sheet
connExcel.Open()
Dim dtExcelSchema As DataTable
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
connExcel.Close()
'Read Data from First Sheet
connExcel.Open()
cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
oda.SelectCommand = cmdExcel
oda.Fill(dt)
connExcel.Close()
'Bind Data to GridView
GridView1.Caption = Path.GetFileName(FilePath)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
Dim FileName As String = GridView1.Caption
Dim Extension As String = Path.GetExtension(FileName)
Dim FilePath As String = Server.MapPath(FolderPath + FileName)
Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
End Sub
Tag : .NET, Ms SQL Server 2008, VBScript, Web (ASP.NET), VS 2010 (.NET 4.x)