2017-08-07 5 views
-2

テキストファイルをSQLサーバーにインポートしようとしています。私はそれをアップロードし、すべてのデータをSQLサーバーに送信するときにファイル内のデータを読み取る必要があります。もちろん、SQLの内部には、新しいデータ行が終了したときに増分する主キーがあります。vb.netのテキストファイルをインポートしてSQLサーバーに挿入する方法は?

HELP!

+0

これまでに何を試しましたか? –

+0

私はまだVBの新人ですが、私はStreamReaderについて読んでいます。私はVBとSQLサーバーの間の接続方法を知っています。 @DIeそれはそれです – ranadev

+0

あなたが言うように、(StreamReaderを使用して)変数に取得した 'fileText'を置くことができるはずです。また、VBアプリケーション内からSQL Serverにアクセスする方法もあります。あなたはSQLServerデータベース上にINSERT文を作成し、パラメータとして 'fileText'を追加する必要があります。それ以上はありません:)例文はたくさんあります –

答えて

0

これは興味深い質問です。私はこれをlooonnnggggの前にしました。あなたの質問の解決方法については、以下のコードを参照してください。

プライベートサブButton4_Click(System.Objectのよう送信者、System.EventArgsとして電子)新のDataTable()

また

tblReadCSV.Columns.Add("FName") 
tblReadCSV.Columns.Add("LName") 
tblReadCSV.Columns.Add("Department") 

Dim csvParser As New TextFieldParser("C:\Users\Excel\Desktop\Employee.txt") 

csvParser.Delimiters = New String() {","} 
csvParser.TrimWhiteSpace = True 
csvParser.ReadLine() 

While Not (csvParser.EndOfData = True) 
    tblReadCSV.Rows.Add(csvParser.ReadFields()) 
End While 

Dim con As New SqlConnection("Server=Excel-PC\SQLEXPRESS;Database=Northwind;Trusted_Connection=True;") 
Dim strSql As String = "Insert into Employee(FName,LName,Department) values(@Fname,@Lname,@Department)" 
'Dim con As New SqlConnection(strCon) 
Dim cmd As New SqlCommand() 
cmd.CommandType = CommandType.Text 
cmd.CommandText = strSql 
cmd.Connection = con 
cmd.Parameters.Add("@Fname", SqlDbType.VarChar, 50, "FName") 
cmd.Parameters.Add("@Lname", SqlDbType.VarChar, 50, "LName") 
cmd.Parameters.Add("@Department", SqlDbType.VarChar, 50, "Department") 

Dim dAdapter As New SqlDataAdapter() 
dAdapter.InsertCommand = cmd 
Dim result As Integer = dAdapter.Update(tblReadCSV) 

End SubのようButton4.Click 点心tblReadCSVを処理し、ALLを参照いくつかの同様の、関連するタスクのために、以下のコードを使用します。通知は、私はいくつかのコメントを追加しましたが、あまりにも多くはない...

Imports System.Data.SqlClient 
Imports System.IO 
Imports Microsoft.VisualBasic.FileIO 
Imports System.Data 
Imports System.Data.Odbc 
Imports System.Data.OleDb 
Imports System.Configuration 


Public Class Form1 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 


     Dim headers = (From header As DataGridViewColumn In DataGridView1.Columns.Cast(Of DataGridViewColumn)() Select header.HeaderText).ToArray 
     Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not row.IsNewRow Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, "")) 
     Dim str As String = "" 
     Using sw As New IO.StreamWriter("C:\Users\Excel\Desktop\OrdersTest.csv") 
      sw.WriteLine(String.Join(",", headers)) 
      'sw.WriteLine(String.Join(",")) 
      For Each r In rows 
       sw.WriteLine(String.Join(",", r)) 
      Next 
      sw.Close() 
     End Using 

    End Sub 

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 
     'Dim m_strConnection As String = "server=Excel-PC\SQLEXPRESS;Initial Catalog=Northwind;Trusted_Connection=True;" 

     'Catch ex As Exception 
     ' MessageBox.Show(ex.ToString()) 
     'End Try 

     'Dim objDataset1 As DataSet() 
     'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     'Dim da As OdbcDataAdapter 
     Dim OpenFile As New System.Windows.Forms.OpenFileDialog ' Does something w/ the OpenFileDialog 
     Dim strFullPath As String, strFileName As String 
     Dim tbFile As New TextBox 
     ' Sets some OpenFileDialog box options 
     OpenFile.Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*" ' Shows only .csv files 
     OpenFile.Title = "Browse to file:" ' Title at the top of the dialog box 

     If OpenFile.ShowDialog() = DialogResult.OK Then ' Makes the open file dialog box show up 
      strFullPath = OpenFile.FileName ' Assigns variable 
      strFileName = Path.GetFileName(strFullPath) 

      If OpenFile.FileNames.Length > 0 Then ' Checks to see if they've picked a file 

       tbFile.Text = strFullPath ' Puts the filename in the textbox 

       ' The connection string for reading into data connection form 
       Dim connStr As String 
       connStr = "Driver={Microsoft Text Driver (*.txt; *.csv)}; Dbq=" + Path.GetDirectoryName(strFullPath) + "; Extensions=csv,txt " 

       ' Sets up the data set and gets stuff from .csv file 
       Dim Conn As New OdbcConnection(connStr) 
       Dim ds As DataSet 
       Dim DataAdapter As New OdbcDataAdapter("SELECT * FROM [" + strFileName + "]", Conn) 
       ds = New DataSet 

       Try 
        DataAdapter.Fill(ds, strFileName) ' Fills data grid.. 
        DataGridView1.DataSource = ds.Tables(strFileName) ' ..according to data source 

        ' Catch and display database errors 
       Catch ex As OdbcException 
        Dim odbcError As OdbcError 
        For Each odbcError In ex.Errors 
         MessageBox.Show(ex.Message) 
        Next 
       End Try 

       ' Cleanup 
       OpenFile.Dispose() 
       Conn.Dispose() 
       DataAdapter.Dispose() 
       ds.Dispose() 

      End If 
     End If 

    End Sub 

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click 
     Dim cnn As SqlConnection 
     Dim connectionString As String 
     Dim sql As String 

     connectionString = "data source=Excel-PC\SQLEXPRESS;" & _ 
     "initial catalog=NORTHWIND;Trusted_Connection=True" 
     cnn = New SqlConnection(connectionString) 
     cnn.Open() 
     sql = "SELECT * FROM [Order Details]" 
     Dim dscmd As New SqlDataAdapter(sql, cnn) 
     Dim ds As New DataSet 
     dscmd.Fill(ds) 
     DataGridView1.DataSource = ds.Tables(0) 

     cnn.Close() 

    End Sub 

    Private Sub ProductsDataGridView_CellFormatting(ByVal sender As Object, 
               ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _ 
               Handles DataGridView1.CellFormatting 

     If e.ColumnIndex = DataGridView1.Columns(3).Index _ 
      AndAlso e.Value IsNot Nothing Then 
      ' 
      If CInt(e.Value) < 10 Then 
       e.CellStyle.BackColor = Color.OrangeRed 
       e.CellStyle.ForeColor = Color.LightGoldenrodYellow 
      End If 
      ' 
     End If 
     ' 
    End Sub 


    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click 
     Dim tblReadCSV As New DataTable() 

     tblReadCSV.Columns.Add("FName") 
     tblReadCSV.Columns.Add("LName") 
     tblReadCSV.Columns.Add("Department") 

     Dim csvParser As New TextFieldParser("C:\Users\Excel\Desktop\Employee.txt") 

     csvParser.Delimiters = New String() {","} 
     csvParser.TrimWhiteSpace = True 
     csvParser.ReadLine() 

     While Not (csvParser.EndOfData = True) 
      tblReadCSV.Rows.Add(csvParser.ReadFields()) 
     End While 

     Dim con As New SqlConnection("Server=Excel-PC\SQLEXPRESS;Database=Northwind;Trusted_Connection=True;") 
     Dim strSql As String = "Insert into Employee(FName,LName,Department) values(@Fname,@Lname,@Department)" 
     'Dim con As New SqlConnection(strCon) 
     Dim cmd As New SqlCommand() 
     cmd.CommandType = CommandType.Text 
     cmd.CommandText = strSql 
     cmd.Connection = con 
     cmd.Parameters.Add("@Fname", SqlDbType.VarChar, 50, "FName") 
     cmd.Parameters.Add("@Lname", SqlDbType.VarChar, 50, "LName") 
     cmd.Parameters.Add("@Department", SqlDbType.VarChar, 50, "Department") 

     Dim dAdapter As New SqlDataAdapter() 
     dAdapter.InsertCommand = cmd 
     Dim result As Integer = dAdapter.Update(tblReadCSV) 

    End Sub 

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click 

     Dim SQLConnectionString As String = "Data Source=Excel-PC\SQLEXPRESS;" & _ 
          "Initial Catalog=Northwind;" & _ 
          "Trusted_Connection=True" 

     ' Open a connection to the AdventureWorks database. 
     Using SourceConnection As SqlConnection = _ 
      New SqlConnection(SQLConnectionString) 
      SourceConnection.Open() 

      ' Perform an initial count on the destination table. 
      Dim CommandRowCount As New SqlCommand(_ 
      "SELECT COUNT(*) FROM dbo.Orders;", _ 
       SourceConnection) 
      Dim CountStart As Long = _ 
       System.Convert.ToInt32(CommandRowCount.ExecuteScalar()) 
      Console.WriteLine("Starting row count = {0}", CountStart) 

      ' Get data from the source table as a AccessDataReader. 
      'Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
      ' "Data Source=" & "C:\Users\Excel\Desktop\OrdersTest.txt" & ";" & _ 
      ' "Extended Properties=" & "text;HDR=Yes;FMT=Delimited"","";" 

      Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
       "Data Source=" & "C:\Users\Excel\Desktop\" & ";" & _ 
       "Extended Properties=""Text;HDR=No""" 

      Dim TextConnection As New System.Data.OleDb.OleDbConnection(ConnectionString) 

      Dim TextCommand As New OleDbCommand("SELECT * FROM OrdersTest#csv", TextConnection) 
      TextConnection.Open() 
      Dim TextDataReader As OleDbDataReader = TextCommand.ExecuteReader(CommandBehavior.SequentialAccess) 

      ' Open the destination connection.    
      Using DestinationConnection As SqlConnection = _ 
       New SqlConnection(SQLConnectionString) 
       DestinationConnection.Open() 

       ' Set up the bulk copy object. 
       ' The column positions in the source data reader 
       ' match the column positions in the destination table, 
       ' so there is no need to map columns. 
       Using BulkCopy As SqlBulkCopy = _ 
        New SqlBulkCopy(DestinationConnection) 
        BulkCopy.DestinationTableName = _ 
        "dbo.Orders" 

        Try 
         ' Write from the source to the destination. 
         BulkCopy.WriteToServer(TextDataReader) 

        Catch ex As Exception 
         Console.WriteLine(ex.Message) 

        Finally 
         ' Close the AccessDataReader. The SqlBulkCopy 
         ' object is automatically closed at the end 
         ' of the Using block. 
         TextDataReader.Close() 
        End Try 
       End Using 

       ' Perform a final count on the destination table 
       ' to see how many rows were added. 
       Dim CountEnd As Long = _ 
        System.Convert.ToInt32(CommandRowCount.ExecuteScalar()) 
       'Console.WriteLine("Ending row count = {0}", CountEnd) 
       'Console.WriteLine("{0} rows were added.", CountEnd - CountStart) 
      End Using 
     End Using 


     'Dim FILE_NAME As String = "C:\Users\Excel\Desktop\OrdersTest.csv" 
     'Dim TextLine As String 

     'If System.IO.File.Exists(FILE_NAME) = True Then 
     ' Dim objReader As New System.IO.StreamReader(FILE_NAME) 
     ' Do While objReader.Peek() <> -1 
     '  TextLine = TextLine & objReader.ReadLine() & vbNewLine 
     ' Loop 
     'Else 
     ' MsgBox("File Does Not Exist") 
     'End If 

     'Dim cn As New SqlConnection("Data Source=Excel-PC\SQLEXPRESS;Initial Catalog=Northwind;Trusted_Connection=True;") 
     'Dim cmd As New SqlCommand 

     'cmd.Connection = cn 

     'cmd.Connection.Close() 
     'cmd.Connection.Open() 

     'cmd.CommandText = "INSERT INTO Orders (OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry) values & OrdersTest.csv" 
     'cmd.ExecuteNonQuery() 
     'cmd.Connection.Close() 

    End Sub 


Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click 
     ' Define the Column Definition        
     Dim dt As New DataTable() 
     dt.Columns.Add("OrderID", GetType(Integer)) 
     dt.Columns.Add("CustomerID", GetType(String)) 
     dt.Columns.Add("EmployeeID", GetType(Integer)) 
     dt.Columns.Add("OrderDate", GetType(Date)) 
     dt.Columns.Add("RequiredDate", GetType(Date)) 
     dt.Columns.Add("ShippedDate", GetType(Date)) 
     dt.Columns.Add("ShipVia", GetType(Integer)) 
     dt.Columns.Add("Freight", GetType(Decimal)) 
     dt.Columns.Add("ShipName", GetType(String)) 
     dt.Columns.Add("ShipAddress", GetType(String)) 
     dt.Columns.Add("ShipCity", GetType(String)) 
     dt.Columns.Add("ShipRegion", GetType(String)) 
     dt.Columns.Add("ShipPostalCode", GetType(String)) 
     dt.Columns.Add("ShipCountry", GetType(String)) 
     Using cn = New SqlConnection("Server=Excel-PC\SQLEXPRESS;Database=Northwind;Trusted_Connection=True;") 
      cn.Open() 
      Dim reader As Microsoft.VisualBasic.FileIO.TextFieldParser 
      Dim currentRow As String() 
      Dim dr As DataRow 
      Dim sqlColumnDataType As String 
      reader = My.Computer.FileSystem.OpenTextFieldParser("C:\Users\Excel\Desktop\OrdersTest.csv") 
      reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited 
      reader.Delimiters = New String() {","} 
      While Not reader.EndOfData 
       Try 
        currentRow = reader.ReadFields() 
        dr = dt.NewRow() 
        For currColumn = 0 To dt.Columns.Count - 1 
         sqlColumnDataType = dt.Columns(currColumn).DataType.Name 
         Select Case sqlColumnDataType 
          Case "String" 
           If String.IsNullOrEmpty(currentRow(currColumn)) Then 
            dr.Item(currColumn) = "" 
           Else 
            dr.Item(currColumn) = Convert.ToString(currentRow(currColumn)) 
           End If 
          Case "Decimal" 
           If String.IsNullOrEmpty(currentRow(currColumn)) Then 
            dr.Item(currColumn) = 0 
           Else 
            dr.Item(currColumn) = Convert.ToDecimal(currentRow(currColumn)) 
           End If 
          Case "DateTime" 
           If String.IsNullOrEmpty(currentRow(currColumn)) Then 
            dr.Item(currColumn) = "" 
           Else 
            dr.Item(currColumn) = Convert.ToDateTime(currentRow(currColumn)) 
           End If 
         End Select 
        Next 
        dt.Rows.Add(dr) 
       Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException 
        MsgBox("Line " & ex.Message & "is not valid." & vbCrLf & "Terminating Read Operation.") 
        reader.Close() 
        reader.Dispose() 
        'Return False 
       Finally 
        dr = Nothing 
       End Try 
      End While 
      Using copy As New SqlBulkCopy(cn) 
       copy.DestinationTableName = "[dbo].[Orders]" 
       copy.WriteToServer(dt) 
      End Using 
     End Using 

    End Sub 

    Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click 

     'Dim cnn As SqlConnection 
     'Dim connectionString As String 
     'Dim sql As String 
     'connectionString = "data source=Excel-PC\SQLEXPRESS;" & _ 
     '"initial catalog=NORTHWIND;Trusted_Connection=True" 
     'cnn = New SqlConnection(connectionString) 
     'cnn.Open() 
     'GetCsvData("C:\Users\Excel\Desktop\OrdersTest.csv", dbo.Orders) 

    End Sub 

    Public Shared Function GetCsvData(ByVal CSVFileName As String, ByRef CSVTable As DataTable) As Boolean 
     Dim reader As Microsoft.VisualBasic.FileIO.TextFieldParser 
     Dim currentRow As String() 
     Dim dr As DataRow 
     Dim sqlColumnDataType As String 
     reader = My.Computer.FileSystem.OpenTextFieldParser(CSVFileName) 
     reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited 
     reader.Delimiters = New String() {","} 
     While Not reader.EndOfData 
      Try 
       currentRow = reader.ReadFields() 
       dr = CSVTable.NewRow() 
       For currColumn = 0 To CSVTable.Columns.Count - 1 
        sqlColumnDataType = CSVTable.Columns(currColumn).DataType.Name 
        Select Case sqlColumnDataType 
         Case "String" 
          If String.IsNullOrEmpty(currentRow(currColumn)) Then 
           dr.Item(currColumn) = "" 
          Else 
           dr.Item(currColumn) = Convert.ToString(currentRow(currColumn)) 
          End If 
         Case "Decimal" 
          If String.IsNullOrEmpty(currentRow(currColumn)) Then 
           dr.Item(currColumn) = 0 
          Else 
           dr.Item(currColumn) = Convert.ToDecimal(currentRow(currColumn)) 
          End If 
         Case "DateTime" 
          If String.IsNullOrEmpty(currentRow(currColumn)) Then 
           dr.Item(currColumn) = "" 
          Else 
           dr.Item(currColumn) = Convert.ToDateTime(currentRow(currColumn)) 
          End If 
        End Select 
       Next 
       CSVTable.Rows.Add(dr) 
      Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException 
       MsgBox("Line " & ex.Message & "is not valid." & vbCrLf & "Terminating Read Operation.") 
       reader.Close() 
       reader.Dispose() 
       Return False 
      Finally 
       dr = Nothing 
      End Try 
     End While 
     reader.Close() 
     reader.Dispose() 
     Return True 
    End Function 

    Private Sub DataGridView1_RowValidating(ByVal sender As Object, ByVal e As DataGridViewCellCancelEventArgs) 


    End Sub 

End Class 

一つ試してより。 。 。 Cinchoo ETL

Public Class Form1 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Dim file As String = "Import.txt" 
     Dim path As String = "C:\Users\path_to_text_file\" 
     Dim ds As New DataSet 
     Try 
      If IO.File.Exists(IO.Path.Combine(path, file)) Then 
       Dim ConStr As String = _ 
       "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ 
       path & ";Extended Properties=""Text;HDR=No;FMT=Delimited\""" 
       Dim conn As New OleDb.OleDbConnection(ConStr) 
       Dim da As New OleDb.OleDbDataAdapter("Select * from " & _ 
       file, conn) 
       da.Fill(ds, "TextFile") 
      End If 
     Catch ex As Exception 
      MessageBox.Show(ex.ToString) 
     End Try 
     DataGridView1.DataSource = ds.Tables(0) 
    End Sub 

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick 

    End Sub 
End Class 
0

- オープンソースETLフレームワークは、以下のように数行のコードでデータベースにファイル全体を読み込むことができます(コードはC#である、あなたがvb.netにそれを翻訳することを願っ)

詳細については
string connectionstring = 
@"Data Source=(localdb)\v11.0;Initial Catalog=TestDb;Integrated Security=True"; 

using (SqlBulkCopy bcp = new SqlBulkCopy(connectionstring)) 
{ 
    using (var dr = new ChoCSVReader<Customer> 
    ("Cust.csv").WithFirstLineHeader().AsDataReader()) 
    { 
     bcp.DestinationTableName = "dbo.Customers"; 
     bcp.EnableStreaming = true; 


     bcp.BatchSize = 10000; 
     bcp.BulkCopyTimeout = 0; 
     bcp.NotifyAfter = 10; 
     bcp.SqlRowsCopied += delegate (object sender, SqlRowsCopiedEventArgs e) 
     { 
      Console.WriteLine(e.RowsCopied.ToString("#,##0") + " rows copied."); 
     }; 
     bcp.WriteToServer(dr); 
    } 
} 

codeproject記事

開示訪問:私はこのライブラリの作者だが。

関連する問題