csvファイルを1行ずつ読み取り、選択した列と行を取得してmysqlデータベースに接続する方法を探しています。
次のコードを作成しましたが、SQL データベースに挿入するために接続できません。
列の時間、名前、場所をすべてVarcharでデータベースに接続するにはどうすればよいですか?
Imports System.IO
Imports System.IO
Imports System.Data.SqlClient
Public Class Form4
Dim con As New SqlConnection
Dim com As New SqlCommand
Dim str As String
Public Sub ReadCSVFile()
Dim fileIn As String = "C:\Users\FFFF\Downloads\new.csv"
Dim fileRows(), fileFields() As String
TextBox1.Text = String.Empty
If File.Exists(fileIn) Then
Dim fileStream As StreamReader = File.OpenText(fileIn)
fileRows = fileStream.ReadToEnd().Split(Environment.NewLine)
For i As Integer = 0 To fileRows.Length - 1
fileFields = fileRows(i).Split(",")
If fileFields.Length >= 4 Then
TextBox1.Text += fileFields(3) & " " & fileFields(2) & " " & fileFields(0) & "<br /> "
End If
Next
Else
TextBox1.Text = fileIn & " not found."
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call ReadCSVFile()
End Sub
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con = New SqlConnection("Data Source=FFF-PC;Initial Catalog=MySolution;User Id=sa;Password=sarfar.811;")
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50, "fileFields(3)")
cmd.Parameters.Add("@Time", SqlDbType.VarChar, 20, "fileFields(2)")
cmd.Parameters.Add("@Location", SqlDbType.VarChar, 50, "fileFields(0)")
Dim strSql As String = "Insert into New_1(Name,Time,Location) values(@Name@Time,@Location)"
Dim dAdapter As New SqlDataAdapter()
dAdapter.InsertCommand = cmd
'Dim result As Integer = dAdapter.Update(ReadingCsv)
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
End Class