Sql 如何在导入到数据库期间合并两个excel列?

Sql 如何在导入到数据库期间合并两个excel列?,sql,excel,vb.net,import-from-excel,Sql,Excel,Vb.net,Import From Excel,我试图在excel中组合日期和时间列,并使用sqlbulkcopy将其映射到数据库中的一列。我得到一个错误: 日期][时间与任何列映射不匹配 请看下面我的示例代码。关于如何在不将其复制到数据表的情况下实现这一点,您有什么想法吗 Dim sSourceConstr As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=

我试图在excel中组合日期和时间列,并使用sqlbulkcopy将其映射到数据库中的一列。我得到一个错误:

日期][时间与任何列映射不匹配

请看下面我的示例代码。关于如何在不将其复制到数据表的情况下实现这一点,您有什么想法吗

Dim sSourceConstr As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", sPath)

Dim sDestConstr As String = ConfigurationManager.ConnectionStrings("SolCards").ConnectionString
Dim sSourceConnection As New OleDbConnection(sSourceConstr)
Using sSourceConnection
    Dim sql As String = String.Format("Select [Customer Cod],[Customer],[PAN],[Vehicle],[Date],[Station],[Driver],[Authorized],[Product]" &
                                          ", [Pump], [Tran No], [Odo], [Metric], [UPrice], [Qty], [Amount], [TimeFormat] FROM [{0}$]", "trans")
    Dim command As New OleDbCommand(sql, sSourceConnection)

    sSourceConnection.Open()
    Using dr As OleDbDataReader = command.ExecuteReader()
        Using bulkCopy As New SqlBulkCopy(sDestConstr)
            bulkCopy.DestinationTableName = "FuelInformation"
            'column mapping               

          bulkCopy.ColumnMappings.Add("[Date] [Time]", "DatePurchased")

            bulkCopy.WriteToServer(dr)
        End Using
    End Using
End Using

此错误的修复程序是:

 "Select ([date],[time])values(@date,@time)"
   bulkCopy.ColumnMappings.Add("@Date", "DatePurchased")
   bulkCopy.ColumnMappings.Add("@time", "DatePurchased")

我使用CStr()实现了它,如下所示,合并这两个列的问题是列标题名包含关键字

 Dim sSourceConstr As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;""", sPath)

    Dim sDestConstr As String = ConfigurationManager.ConnectionStrings("SolCards").ConnectionString
    Dim sSourceConnection As New OleDbConnection(sSourceConstr)
    Using sSourceConnection
        Dim sql As String = String.Format("SELECT CStr([Date]) + ' ' + CStr([Time]) as [DatePurchased] ,[Customer Cod],[Customer],[PAN],[Vehicle],[Station],[Driver],[Authorized],[Product], [Pump], [Tran No], [Odo], [Metric], [UPrice], [Qty], [Amount], [TimeFormat] FROM [{0}$]", "trans")
        Dim command As New OleDbCommand(sql, sSourceConnection)

        sSourceConnection.Open()
        Using dr As OleDbDataReader = command.ExecuteReader()
            Using bulkCopy As New SqlBulkCopy(sDestConstr)
                bulkCopy.DestinationTableName = "FuelInformation"
                'column mapping 
                bulkCopy.ColumnMappings.Add("[DatePurchased]", "DatePurchased")

                bulkCopy.WriteToServer(dr)
            End Using
        End Using

    End Using

我解决这个问题的方法是使用excel实际连接公式。您可以将其放入代码中,也可以在电子表格中添加一个新列并调用该单元格的值。

选择[Customer Cod]+'+[Customer]
-这是组合列。当我遇到此错误时,“提供商无法确定日期时间值”@zackraiyan是否有包含日期和时间的列?嘿,等等!
bulkCopy.ColumnMappings.Add(“[date][time]”,DatePurchased”)
…这不起作用。你需要声明与我的答案相关的[date],[time]是什么。我得到了这个错误:查询表达式“([date],[time])值(@date中的语法错误(缺少运算符)这是我的选择语句:“从[{0}$]、“trans”中选择[客户Cod]、[客户]、[PAN]、[车辆]、([日期]、[时间])值(@Date、@Time)、[车站]、[司机]、[授权]、[产品]、[泵]、[运输编号]、[里程]、[数量]、[金额]、[时间格式]”