Convert Data from SQL Server to DBF file (Date Error)

╄→гoц情女王★ 提交于 2021-01-29 18:00:36

问题


I want to export data from sql server to dbf file. Below query is working without date. But when I add date it's not working. Please solve my issue, thanks in advance. [Error is : Error in INSERT INTO]

Dim dv As DataView = datacls.fnGetdata(sql_X, ClsDataConnection.DMode.D_DataView)
Dim dt As New DataTable ' // assuming this is already populated from your SQL Database.
Dim buildInsert As StringBuilder = New StringBuilder
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\a\;Extended Properties=dBASE IV;"

Dim connection As OleDbConnection = New OleDbConnection(connectionString)
Dim scriptCommand As OleDbCommand = connection.CreateCommand

Dim str_sr_no As String = ""
Dim str_date As String = ""
Dim str_book As String = ""
Dim str_vchseries As String = ""
Dim str_vchno As String = ""
Dim str_accode As String = ""
Dim str_achead As String = ""
Dim str_city As String = ""
Dim str_narration As String = ""
Dim str_debit As String = ""
Dim str_credit As String = ""


For x = 0 To dv.Count - 1
    'sr_No ,  DT, 'B' Book, 'B' VCHseries,   [VCH No], [Accode], '' [AcHead], [City], narration,  debit , credit
    str_sr_no = dv(x).Item("lineno").ToString
    str_date = Format(CDate(dv(x).Item("date")), "MM/dd/yyyy")
    str_book = dv(x).Item("Book").ToString
    str_vchseries = dv(x).Item("VCHseries").ToString
    str_vchno = dv(x).Item("VCHNo").ToString
    str_accode = dv(x).Item("Accode").ToString
    str_achead = dv(x).Item("AcHead").ToString
    str_city = dv(x).Item("City").ToString
    str_narration = dv(x).Item("narration").ToString
    str_debit = dv(x).Item("debit").ToString
    str_credit = dv(x).Item("credit").ToString
    Dim ndt As Date = Date.Now()

    scriptCommand.CommandType = CommandType.Text
    scriptCommand.CommandText = "INSERT INTO Fe130328.DBF (LINENO, DATE,BOOK,VCHSERIES,VCHNO,ACCODE,ACHEAD,CITY,NARRATION,DEBIT,CREDIT) VALUES ('" & str_sr_no & "', '" & ndt & "','" & str_book & "','" & str_vchseries & "','" & str_vchno & "','" & str_accode & "','" & str_achead & "','" & str_city & "','" & str_narration & "','" & str_debit & "','" & str_credit & "')"

    connection.Open()
    scriptCommand.ExecuteNonQuery()
    connection.Close()
Next

回答1:


Here's the proper way to get data from one database into another:

Dim table As New DataTable

Using sourceAdapter As New OleDbDataAdapter("SELECT * FROM SourceTable", "source connection string here") With {.AcceptChangesDuringFill = False}
    sourceAdapter.Fill(table)
End Using

Using destinationConnection As New OleDbConnection("destination connection string here"),
      destinationCommand As New OleDbCommand("INSERT INTO DestinationTable ([Column1], [Column2], [Column3]) VALUES (@Column1, @Column2, @Column2)", destinationConnection),
      destinationAdapter As New OleDbDataAdapter With {.InsertCommand = destinationCommand}
    With destinationCommand.Parameters
        .Add("@Column1", OleDbType.Integer, 0, "Column1")
        .Add("@Column2", OleDbType.VarChar, 50, "Column2")
        .Add("@Column3", OleDbType.Date, 0, "Column3")
    End With

    destinationAdapter.Update(table)
End Using

You just modify the SQL code and parameters as required and set the connection strings. Note that I have escaped the column names in the SQL code, which is required if you use reserved words or special characters. The parameter prefix and identifier escape characters may vary from database to database.

I should also point out the importance of setting AcceptChangesDuringFill to False. If you don't, AcceptChanges will be called implicitly by the Fill method and all your DataRows will be Unchanged, so there will be nothing to insert. By not calling AcceptChanges, you leave every row Added and ready to be inserted.



来源:https://stackoverflow.com/questions/63374823/convert-data-from-sql-server-to-dbf-file-date-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!