LoginSignup
1
1

More than 1 year has passed since last update.

CSVデータをSQLServerにインポートする(DataTable経由)

Posted at

ここではカラムをidとnameの2つとする。

sample.vb
        'ダイアログを開く
        Using ofd As OpenFileDialog = New OpenFileDialog
            'デフォルトのファイル名を指定する
            ofd.FileName = "新規社員リスト.csv"

            Dim filename As String = ofd.FileName

            '選択できるファイルの種類(拡張子)を限定する
            ofd.Filter = "CSV Files |*.csv"

            If ofd.ShowDialog() = DialogResult.OK Then

                Using sr As New StreamReader(ofd.FileName, Encoding.UTF8)

                    Dim dt As DataTable
                    Dim dr As DataRow
                    Dim i As Integer

                    dt = New DataTable("DataTableHoge")
                    dt.Columns.Add(New DataColumn("id", GetType(String)))
                    dt.Columns.Add(New DataColumn("name", GetType(String)))

                    Using textParser As New TextFieldParser($"C:\Users\Desktop\{filename}", System.Text.Encoding.GetEncoding("Shift-JIS"))

                        'CSVファイル
                        textParser.TextFieldType = FieldType.Delimited

                        '区切り文字
                        textParser.SetDelimiters(",")

                        'ファイルの終端までループ
                        While Not textParser.EndOfData

                            '1行読み込み
                            Dim row As String() = textParser.ReadFields()

                            'DataRow作成
                            i = 0
                            dr = dt.NewRow()
                            For Each col As String In row
                                dr(i) = col
                                i += 1
                            Next

                            'DataTableに追加
                            dt.Rows.Add(dr)
                        End While
                    End Using

                    ' SQLServerに接続する
                    Dim con As New SqlConnection(
                    "Data Source=HOGE\SQLEXPRESS;" +
                    "Initial Catalog=hogehoge;" +
                    "Integrated Security=True")
                    Dim cmd As New SqlCommand
                    Dim sql As New StringBuilder
                    con.Open()
                    cmd.Connection = con

                    sql.AppendLine("INSERT INTO dbo.HOGE ")
                    sql.AppendLine("( ")
                    sql.AppendLine("  id, name ")
                    sql.AppendLine(") VALUES ( ")
                    sql.AppendLine("  @id, @name ")
                    sql.AppendLine("); ")

                    For n As Integer = 0 To dt.Rows.Count - 1
                        cmd.Parameters.Clear()

                        'パラメータの作成
                        cmd.Parameters.Add("@id", SqlDbType.Int).Value = dt.Rows(n).Item(0)
                        cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = dt.Rows(n).Item(1)

                        'プロシージャの実行
                        cmd.CommandType = CommandType.Text
                        cmd.CommandText = sql.ToString()
                        cmd.ExecuteNonQuery()

                    Next n
                End Using
            End If
        End Using
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1