我正在整理一些代码以将Excel数据导入数据库。我可以使用Excel导入,但是需要添加两个常量列,一个数字,一个字母数字。数字列可以很好地插入,但字母数字列会因错误而崩溃...
System.Data.dll中发生了'System.Data.OleDb.OleDbException'类型的异常,但未在用户代码中处理 附加信息:没有为一个或多个必需参数提供值。
如果我将字母数字字符串的测试值更改为数字,则该过程有效。 如果我将SQL语句中声明的字符串替换为纯文本,则该过程有效。
我的嵌入式SQL语句如下,我正在努力使用的值名为“ CBUser”
Using oda As New OleDbDataAdapter((Convert.ToString("SELECT " + ImportRecord.ToString + " AS ImportRecord,[expected],[SuppNo],[Supplier],[PNo],[Product],[PONo],[ord#css#]," + CBUser.ToString + " AS CBUsername,(( Date() & ' ' & Time()))AS Created FROM [") & sheet1) + "] WHERE expected IS NOT NULL", excel_con)
有什么主意我想念的地方吗?完整代码如下。谢谢。
connString = String.Format(connString, excelPath)
Using excel_con As New OleDbConnection(connString)
excel_con.Open()
Dim sheet1 As String = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing).Rows(0)("TABLE_NAME").ToString()
Dim ImportRecord As Integer = "111"
Dim CBUser As String = "Test"
Dim dtExcelData As New DataTable()
dtExcelData.Columns.AddRange(New DataColumn(9) {New DataColumn("ImportFile_id", GetType(Integer)),
New DataColumn("DeliveryDate", GetType(Date)),
New DataColumn("SupplierNumber", GetType(Integer)),
New DataColumn("SupplierName", GetType(String)),
New DataColumn("ProductCode", GetType(Integer)),
New DataColumn("ProductDescription", GetType(String)),
New DataColumn("PurchaseOrderNumber", GetType(Integer)),
New DataColumn("Cases", GetType(Integer)),
New DataColumn("CreatedBy", GetType(String)),
New DataColumn("CreatedOn", GetType(DateTime))})
Using oda As New OleDbDataAdapter((Convert.ToString("SELECT " + ImportRecord.ToString + " AS ImportRecord,[expected],[SuppNo],[Supplier],[PNo],[Product],[PONo],[ord#css#]," + CBUser.ToString + " AS CBUsername,(( Date() & ' ' & Time()))AS Created FROM [") & sheet1) + "] WHERE expected IS NOT NULL", excel_con)
oda.Fill(dtExcelData)
End Using
excel_con.Close()
Dim conString As String = ConfigurationManager.ConnectionStrings("Company Database").ConnectionString
Using con As New SqlConnection(conString)
Using sqlBulkCopy As New SqlBulkCopy(con)
sqlBulkCopy.DestinationTableName = "dbo.OrderImports"
sqlBulkCopy.ColumnMappings.Add("ImportRecord", "ImportFile_id")
sqlBulkCopy.ColumnMappings.Add("expected", "DeliveryDate")
sqlBulkCopy.ColumnMappings.Add("SuppNo", "SupplierNumber")
sqlBulkCopy.ColumnMappings.Add("Supplier", "SupplierName")
sqlBulkCopy.ColumnMappings.Add("PNo", "ProductCode")
sqlBulkCopy.ColumnMappings.Add("Product", "ProductDescription")
sqlBulkCopy.ColumnMappings.Add("PONo", "PurchaseOrderNumber")
sqlBulkCopy.ColumnMappings.Add("ord#css#", "Cases")
sqlBulkCopy.ColumnMappings.Add("CBUsername", "CreatedBy")
sqlBulkCopy.ColumnMappings.Add("Created", "CreatedOn")
con.Open()
sqlBulkCopy.WriteToServer(dtExcelData)
con.Close()
End Using
End Using
End Using