Private Sub btupload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btupload.Click
Dim myConnectionString As String = "server=.\SQLEXPRESS;" & _
"integrated security=sspi;database=logfileSQL"
Dim myConnection As SqlConnection = New SqlConnection(myConnectionString)
Dim csvInputFile As String = "C:\logfile_mysql.csv"
Dim cmd As SqlCommand = myConnection.CreateCommand()
Dim dt As New DataTable()
Dim line As String = Nothing
Dim i As Integer = 0
Using sr As StreamReader = File.OpenText(csvInputFile)
line = sr.ReadLine()
Do While line IsNot Nothing
Dim data() As String = line.Split(","c)
If data.Length > 0 Then
If i = 0 Then
For Each item In data
dt.Columns.Add(New DataColumn())
Next item
i = 1
End If
Dim row As DataRow = dt.NewRow()
row.ItemArray = data
dt.Rows.Add(row)
End If
line = sr.ReadLine()
dgv1.DataSource = dt
Loop
End Using
myConnection.Open()
Dim bc As New SqlBulkCopy(myConnection)
MsgBox(dt.Rows.Count)
bc.BatchSize = dt.Rows.Count
bc.DestinationTableName = "transactionsanalyzer"
bc.WriteToServer(dt)
bc.Close()
myConnection.Close()
dgv1.DataSource = dt
End Sub
Tag : .NET, Ms SQL Server 2005, VB.NET, VS 2008 (.NET 3.x)
Private Sub btupload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btupload.Click
Dim myConnectionString As String = "server=.\SQLEXPRESS;" & _
"integrated security=sspi;database=logfileSQL"
Dim myConnection As SqlConnection = New SqlConnection(myConnectionString)
Dim csvInputFile As String = "C:\logfile_mysql.csv"
Dim cmd As SqlCommand = myConnection.CreateCommand()
Dim dt As New DataTable()
Dim line As String = Nothing
Dim i As Integer = 0
Using sr As StreamReader = File.OpenText(csvInputFile)
line = sr.ReadLine()
Do While line IsNot Nothing
Dim data() As String = line.Split(","c)
If data.Length > 0 Then
If i = 0 Then
For Each item In data
dt.Columns.Add(New DataColumn())
Next item
i = 1
End If
Dim row As DataRow = dt.NewRow()
row.ItemArray = data
dt.Rows.Add(row)
End If
line = sr.ReadLine()
dgv1.DataSource = dt
Loop
End Using
myConnection.Open()
Dim bc As New SqlBulkCopy(myConnection)
MsgBox(dt.Rows.Count)
bc.BatchSize = dt.Rows.Count
bc.DestinationTableName = "transactionsanalyzer"
bc.WriteToServer(dt) ' error ว่า convert parameter value from a String to a DateTime แก้ยังไงดีครับ ผมทำไม่ถูก
bc.Close()
myConnection.Close()
dgv1.DataSource = dt
End Sub