| 
 | 
 
  
 
 
ถามว่าให้ Excel ทำงานเป็นเหมือนฐานข้อมูล หรือ DataBase ได้หรือไม่ แอดมินตอบได้เลยว่าทำได้ครับ แต่ไม่ค่อยเหมาะสมเท่าใดนัก เพราะใช้งานผิดวัตถุประสงค์น่ะครับ แต่ในบางกรณีบางคนอาจจะมีชุดข้อมูลจาก Excel อยู่แล้ว ก็ไม่อยากจะย้ายข้อมูลไปเก็บที่ Access หรืออื่นๆ โค้ดชุดนี้แอดมินจะสาธิตและนำทางให้แบบเบื้องต้น คือ การเพิ่มข้อมูลเข้าไปใน Excel โดยใช้ ADO.NET แทน Excel Object ครับผม ...  
มาดูโค้ดกันเถอะ ...  
- ' / --------------------------------------------------------------------------------
 
 - ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
 
 - ' / eMail : thongkorn@hotmail.com
 
 - ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
 
 - ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
 
 - ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
 
 - ' / MORE: http://www.g2gnet.com/webboard
 
 - ' /
 
 - ' / Microsoft Excel is able to perform back ends provide.
 
 - ' / Microsoft Visual Basic .NET (2010)
 
 - ' /
 
 - ' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
 
 - ' / You can modify and/or distribute without to inform the developer.
 
 - ' / --------------------------------------------------------------------------------
 
 - Imports System.Data.OleDb
 
  
- Public Class frmExcelDBBackEnd
 
 -     '// Path and File name of Excel
 
 -     Dim strFilename As String
 
  
-     ' / --------------------------------------------------------------------------------
 
 -     ' / Get my project path
 
 -     ' / AppPath = C:\My Project\bin\debug
 
 -     ' / Replace "\bin\debug" with ""
 
 -     ' / Return : C:\My Project\
 
 -     Function MyPath(ByVal AppPath As String) As String
 
 -         '/ MessageBox.Show(AppPath);
 
 -         AppPath = AppPath.ToLower()
 
 -         '/ Return Value
 
 -         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "")
 
 -         '// If not found folder then put the \ (BackSlash) at the end.
 
 -         If Microsoft.VisualBasic.Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
 
 -     End Function
 
  
-     Private Function CreateConnString(ByVal strFilename As String) As String
 
 -         Return "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " + strFilename + "; Extended Properties=""Excel 8.0;HDR=YES"";"
 
 -     End Function
 
  
-     Private Sub frmExcelDBBackEnd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
 -         Call ClearScreen()
 
 -         '// Create Excel File
 
 -         strFilename = MyPath(Application.StartupPath) + "Book1.xls"
 
 -         '// Check file exists.
 
 -         If Not (My.Computer.FileSystem.FileExists(strFilename)) Then
 
 -             '// Not found
 
 -             Dim Conn As New OleDbConnection
 
 -             Dim Comm As New OleDbCommand
 
  
-             Conn.ConnectionString = CreateConnString(strFilename)
 
 -             Comm.Connection = Conn
 
 -             '// Create a new sheet consisting of three columns.
 
 -             Comm.CommandText = "Create Table MySheet (MemberID Char(255), Firstname Char(255), Lastname Char(255), MemberShip Char(255))"
 
 -             Try
 
 -                 Conn.Open()
 
 -                 Comm.ExecuteNonQuery()
 
 -                 MsgBox("Created MS Excel successful.")
 
 -                 Conn.Close()
 
 -             Catch ex As Exception
 
 -                 MsgBox("Error: " & ex.Message)
 
 -                 Conn.Close()
 
 -             End Try
 
 -         Else
 
 -             MessageBox.Show("The Excel file exists.", "Report status", MessageBoxButtons.OK, MessageBoxIcon.Information)
 
 -         End If
 
 -     End Sub
 
  
-     Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
 
 -         If Trim(txtMemberID.Text.Length) = 0 Or Trim(txtMemberID.Text) = "" Then
 
 -             MessageBox.Show("Please enter Member ID.", "Report status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
 
 -             txtMemberID.Focus()
 
 -             Return
 
 -         End If
 
  
-         Dim Conn As New OleDbConnection
 
 -         Dim Comm As New OleDbCommand
 
 -         Conn.ConnectionString = CreateConnString(MyPath(Application.StartupPath) + "Book1.xls")
 
 -         Comm.Connection = Conn
 
 -         Dim strSQL As String = _
 
 -                 " INSERT INTO [MySheet$] (" & _
 
 -                 " MemberID, Firstname, Lastname, MemberShip) " & _
 
 -                 " VALUES (" & _
 
 -                 "'" & txtMemberID.Text & "'," & _
 
 -                 "'" & txtFirstname.Text & "'," & _
 
 -                 "'" & txtLastname.Text & "'," & _
 
 -                 "'" & cmbMembership.Text & "'" & _
 
 -                 ")"
 
 -         Comm.CommandText = strSQL
 
 -         Try
 
 -             Conn.Open()
 
 -             Comm.ExecuteNonQuery()
 
 -             MsgBox("Inserted new record successful.")
 
 -             Conn.Close()
 
 -             '// Clear screen
 
 -             Call ClearScreen()
 
  
-         Catch ex As Exception
 
 -             MsgBox("Error: " & ex.Message)
 
 -             Conn.Close()
 
 -         End Try
 
 -     End Sub
 
  
-     Private Sub ClearScreen()
 
 -         '// Clear
 
 -         txtMemberID.Clear()
 
 -         txtFirstname.Clear()
 
 -         txtLastname.Clear()
 
 -         txtMemberID.Focus()
 
 -         '//
 
 -         With cmbMembership
 
 -             .Items.Clear()
 
 -             .Items.Add("Normal")
 
 -             .Items.Add("Level I")
 
 -             .Items.Add("Level II")
 
 -             .Items.Add("Level III")
 
 -             .Items.Add("VIP")
 
 -             .SelectedIndex = 0
 
 -         End With
 
 -     End Sub
 
  
-     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
 
 -         Me.Close()
 
 -     End Sub
 
  
-     Private Sub frmExcelDBBackEnd_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
 
 -         Me.Dispose()
 
 -         Application.Exit()
 
 -     End Sub
 
  
- End Class
 
  คัดลอกไปที่คลิปบอร์ด 
 
 
ดาวน์โหลดโค้ดต้นฉบับตัวเต็ม VB.NET (2010) ได้ที่นี่ ...  
 
 
 
 |   
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน  
 
x
 
 
 
 
 |