简体   繁体   中英

Cannot insert Data from CSV into SQL table

I have the following code to import data from a CSV file to a sql table. When I run the code no error are displayed. However nothing is inserted into the SQL table.

    Dim folder = "C:\Users\Administrator\Desktop\SKEDULERING\Data\"
    Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
    Dim dt As New DataTable

    Using Adp As New OleDbDataAdapter("select * from [labanal2.csv]", CnStr)

        Adp.Fill(dt)
    End Using


    Dim strsql As String = "insert into ontledings (PLAASNO,PLAASNAAM,BLOKNO,AREA,NAME3,KULTIVAR,WINGKLAS,ANALISEDATUM,SUIKER,pH,SUUR) values (@PLAASNO,@PLAASNAAM,@BLOKNO,@AREA,@NAME3,@KULTIVAR,@WINGKLAS,@ANALISEDATUM,@SUIKER,@pH,@SUUR)"

    Dim SqlconnectionString As String = "Data Source=GIDEON-E-LAPTOP\SQLEXPRESS2014;Initial Catalog=SkeduleringDatabasis;Integrated Security=True"

    Using connection As New SqlClient.SqlConnection(SqlconnectionString)

        Dim cmd As New SqlClient.SqlCommand(strsql, connection)

        With cmd.Parameters



            .Add("@PLAASNO", SqlDbType.VarChar, 50, "PLAASNO")
            .Add("@PLAASNAAM", SqlDbType.VarChar, 50, "PLAASNAAM")
            .Add("@BLOKNO", SqlDbType.VarChar, 50, "BLOKNO")
            .Add("@AREA", SqlDbType.VarChar, 50, "AREA")
            .Add("@ANALISEDATUM", SqlDbType.Date, 50, "ANALISEDATUM")
            .Add("@NAME3", SqlDbType.VarChar, 50, "NAME3")
            .Add("@KULTIVAR", SqlDbType.VarChar, 50, "KULTIVAR")
            .Add("@WINGKLAS", SqlDbType.VarChar, 50, "WINGKLAS")
            .Add("@SUIKER", SqlDbType.Decimal, 50, "SUIKER")
            .Add("@pH", SqlDbType.Decimal, 50, "pH")
            .Add("@SUUR", SqlDbType.Decimal, 50, "SUUR")


        End With

        Dim adapter As New SqlClient.SqlDataAdapter()
        adapter.InsertCommand = cmd
        Dim iRowsInserted As Int32 = adapter.Update(dt)

        MsgBox("Klaar OLEB")
    End Using

What am I missing?

The problem is caused by the fact that each row in your DataTable has the RowState = DataRowState.Unchanged. This means that each row is simply ignored when you call Update.

This is the normal behavior expected by the Update method, only the rows in state Added, Changed or Deleted are taken in consideration for the Insert, Update or Delete command.

You can add this loop before calling update

For Each row As DataRow In dt.Rows
    row.SetAdded()
Next

Or, as explained below by Plutonix, change the behavior of the Fill method setting the property AcceptChangesDuringFill to False

Using Adp As New OleDbDataAdapter("select * from [labanal2.csv]", CnStr)
    Adp.AcceptChangesDuringFill = False
    Adp.Fill(dt)
End Using

This will leave the RowState property with the DataRowState.Added flag active.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM