ตัวอย่างนะค่ะ
a b c d
124567964 124564 1246 63
145674973 456793 7896 75
124597895 124567 1265 59 ข้อมูลพวกนี้จะอยูใน textbox ที่เปิดจากไฟล์.txt
แ]tในฐานข้อมูลที่เรียกมาจะมีคอลัม a b c คือจะเอาสตริง a b cไปเปรียบเทียบกับข้อมูลจากฐานข้อมูลที่ดึงมาตอนนี้ถ้าเกิดข้อมูลตรงกันให้บันทึกข้อมูลที่คอลัม dลงไปในฐานข้อมูลที่คอลัม a b c ตรงกันอ่ะค่ะ
#Region "Engine"
Public Class ReadData
Public Shared Function ReadDataFromFile(ByVal e As ReadDataArgs) As Integer
Dim iRet As Integer
If (Not System.IO.File.Exists(e.TextFileName)) Then
Return -1
End If
Using myReader As System.IO.StreamReader = New System.IO.StreamReader(e.TextFileName)
Dim myAccessDatabase As MsAccessDataUtils = New MsAccessDataUtils(e.DataBaseFileName, e.UserName, e.Password)
'send to database
Dim CommandStrBuilder As System.Text.StringBuilder = New System.Text.StringBuilder()
CommandStrBuilder.AppendLine("UPDATE [{0}] SET [{1}]=@ValueData ")
CommandStrBuilder.AppendLine("WHERE ([{2}]=@fieldAValue) ")
CommandStrBuilder.AppendLine(" AND ([{3}]=@fieldBValue) AND ([{4}]=@fieldCValue)")
Dim cmdStr As String = CommandStrBuilder.ToString()
cmdStr = String.Format(cmdStr, e.TableName _
, e.ValueColumnName, e.ColumnAName, e.ColumnBName, e.ColumnCName)
Dim myLine As String = String.Empty
iRet = 0
Do
myLine = myReader.ReadLine()
'Split incoming string with space charactor
Dim FragmentStr() As String = myLine.Split(New [Char]() {" "c}, StringSplitOptions.RemoveEmptyEntries)
'prepare data fro sendinto command
Dim ColumnValue() As String = New String() {String.Empty, String.Empty, String.Empty, String.Empty}
'trap some error here
Try
For i As Integer = 0 To 3
ColumnValue(i) = FragmentStr(i)
Next
Catch ex As Exception
End Try
Dim myCommandParams As System.Collections.Hashtable = New System.Collections.Hashtable()
myCommandParams.Add("@ValueData", ColumnValue(3))
myCommandParams.Add("@fieldAValue", ColumnValue(0))
myCommandParams.Add("@fieldBValue", ColumnValue(1))
myCommandParams.Add("@fieldCValue", ColumnValue(2))
myAccessDatabase.ExecuteMsAccessSQLCommand(cmdStr, myCommandParams)
iRet += 1
Loop While (myLine Is Nothing)
End Using
Return iRet
End Function
End Class
#End Region
#Region "Supplement Class"
Public Class ReadDataArgs
Public TextFileName As String = String.Empty
Public DataBaseFileName As String = String.Empty
Public UserName As String = String.Empty
Public Password As String = String.Empty
Public TableName As String = String.Empty
Public ColumnAName As String = String.Empty
Public ColumnBName As String = String.Empty
Public ColumnCName As String = String.Empty
Public ValueColumnName As String = String.Empty
Public Sub New()
End Sub
Public Sub New(ByVal argTextFileName As String, ByVal argDataBaseFileName As String _
, ByVal argUserName As String, ByVal argPassword As String _
, ByVal argTableName As String _
, ByVal argColumnAName As String, ByVal argColumnBName As String, ByVal argColumnCName As String _
, ByVal argValueColumnName As String)
TextFileName = argTextFileName
DataBaseFileName = argDataBaseFileName
UserName = argUserName
Password = argPassword
TableName = argTableName
ColumnAName = argColumnAName
ColumnBName = argColumnBName
ColumnCName = argColumnCName
ValueColumnName = argValueColumnName
End Sub
End Class
Public Class MsAccessDataUtils
Private Const constConnectionStringFrame = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};"
Private Const constConnectionStringFrameWithUserNameAndPassword = constConnectionStringFrame & "User Id={1};Password={2};"
Private Const constMsAccessCommandParameterPrefix As String = "@"
Private _DataBaseFile As String = String.Empty
Private _HasUserName As Boolean = False
Private _UserName As String = String.Empty
Private _Password As String = String.Empty
Public Property DataBaseFile()
Get
Return _DataBaseFile
End Get
Set(ByVal value)
_DataBaseFile = value
If ((_UserName.Length = 0) And (_Password.Length = 0)) Then
_HasUserName = False
Else
_HasUserName = True
End If
End Set
End Property
Public Property UserName() As String
Get
Return _UserName
End Get
Set(ByVal value As String)
_UserName = value
If ((_UserName.Length = 0) And (_Password.Length = 0)) Then
_HasUserName = False
Else
_HasUserName = True
End If
End Set
End Property
Public Property Password()
Get
Return _Password
End Get
Set(ByVal value)
_Password = value
If ((_UserName.Length = 0) And (_Password.Length = 0)) Then
_HasUserName = False
Else
_HasUserName = True
End If
End Set
End Property
Public Property HasUserNameAndPassword()
Get
Return _HasUserName
End Get
Set(ByVal value)
_HasUserName = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(ByVal argDatabaseName As String)
Me.DataBaseFile = argDatabaseName
End Sub
Public Sub New(ByVal argDatabaseName As String, ByVal argUserName As String, ByVal argPassword As String)
Me.DataBaseFile = argDatabaseName
Me.UserName = argUserName
Me.Password = argPassword
End Sub
Public Shared ConnectionString As String = String.Empty
Private Function ConnectionStringBuilder() As String
Dim retStr As String = String.Empty
If (Me.HasUserNameAndPassword) Then
retStr = String.Format(constConnectionStringFrameWithUserNameAndPassword _
, Me.DataBaseFile, Me.UserName, Me.Password)
Else
retStr = String.Format(constConnectionStringFrame _
, Me.DataBaseFile)
End If
Return retStr
End Function
Public Sub ExecuteMsAccessSQLCommand(ByVal argCommandText As String, ByVal Params As System.Collections.Hashtable)
Using myConnection As System.Data.OleDb.OleDbConnection _
= New System.Data.OleDb.OleDbConnection(Me.ConnectionStringBuilder())
Dim myCommand As System.Data.OleDb.OleDbCommand _
= New System.Data.OleDb.OleDbCommand(argCommandText, myConnection)
myCommand.CommandType = CommandType.Text
If (Not (Params Is Nothing)) And (Params.Count > 0) Then
For Each keyStr As String In Params.Keys
Dim tempKey As String = keyStr
If (Not keyStr.Substring(0, 1).Equals(constMsAccessCommandParameterPrefix)) Then
tempKey = constMsAccessCommandParameterPrefix & tempKey
End If
myCommand.Parameters.AddWithValue(tempKey, Params(keyStr))
Next
End If
Try
myConnection.Open()
myCommand.ExecuteNonQuery()
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
If Not (myConnection Is Nothing) Then
myConnection.Close()
End If
End Try
End Using
End Sub
End Class
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim myReadDataArgs As ReadDataArgs = New ReadDataArgs()
With myReadDataArgs
'ชื่อและตำแหน่งของ text file
.TextFileName = "C:\SAMPLE.TXT"
'ชื่อและตำแหน่งของ ฐานข้อมูล access
.DataBaseFileName = "C:\DB.ACCDB"
'ชื่อ ตารางที่ใช้เก็บข้อมูล
.TableName = "SAMPLENAME" '
'ชื่อ column A B C D จากโจทย์ ทั้งหมดก็ปรับตามการใช้งานของคุณค่ะ
.ColumnAName = "A"
.ColumnBName = "B"
.ColumnCName = "C"
.ValueColumnName = "D"
End With
ReadData.ReadDataFromFile(myReadDataArgs )
End Sub