我需要有關使用 VB.NET 將 Excel 檔案匯入 SQL Server 的幫助。我的編碼運行良好,但有時會出現一條前訊息,說轉換日期失敗

這里的錯誤。當我第一次匯入它時,它作業正常,但在更改 excel 中的主鍵和其他事情后,它在日期出錯

這是 Excel 檔案中的日期。第一次有效,但第二次無效。我在 Excel 中寫日期就像 SQL Server 日期格式,如 2021-12-14,即 YYYY-MM-DD。我現在對此有點困惑了一個月......如果我在 Excel 中有 10 行,有時會發生關于轉換日期但仍將資料匯入 SQL Server 但其中一些未匯入 sql 的錯誤
Try
OLEcon.Open()
With OLEcmd
.Connection = OLEcon
.CommandText = "select * from [Sheet1$]"
End With
OLEda.SelectCommand = OLEcmd
OLEda.Fill(OLEdt)
For Each r As DataRow In OLEdt.Rows
Dim intAge As Integer
intAge = Convert.ToInt32(r(2).ToString)
Dim dateLED As Date
dateLED = Convert.ToDateTime(r(11).ToString)
Dim dateDJ As Date
dateDJ = Convert.ToDateTime(r(12).ToString)
sql = "INSERT INTO MasterStaffListTry (EENo,Name,Age,AgeCategory,Gender,Ethnicity,Grade,Category,Department,Position,ReportingTo,LastEmploymentDate,DateJoin,LOCUM,Status) VALUES
('" & r(0).ToString & "','" & r(1).ToString & "','" & intAge & "','" & r(3).ToString & "','" & r(4).ToString & "',
'" & r(5).ToString & "' ,'" & r(6).ToString & "','" & r(7).ToString & "','" & r(8).ToString & "','" & r(9).ToString & "',
'" & r(10).ToString & "','" & dateLED.ToShortDateString & "','" & dateDJ.ToShortDateString & "','" & r(13).ToString & "' ,'" & r(14).ToString & "')"
resul = saveData(sql)
If resul Then
Timer1.Start()
End If
Next
這是我匯入 Excel 檔案的編碼。我認為這里是錯誤的部分。
uj5u.com熱心網友回復:
您可以使代碼更高效并添加錯誤檢查。
您可以僅使用 SQL 命令的一個實體并更改其引數值以提交新資料。
在獲取這些引數的值時,您可以使用類似 的函式DateTime.TryParse,這使您有機會根據需要處理決議錯誤 - 您可以跳過該行,或添加到錯誤日志中,而不是嘗試插入無效資料。
你可以從這個例子開始:
'TODO: add all the parameters and set their .SqlDbType and .Size values to match the database columns.
Dim eeNo = New SqlParameter With {.ParameterName = "@EENo", .SqlDbType = SqlDbType.NVarChar, .Size = 16}
Dim name = New SqlParameter With {.ParameterName = "@Name", .SqlDbType = SqlDbType.NVarChar, .Size = 60}
Dim age = New SqlParameter With {.ParameterName = "@Age", .SqlDbType = SqlDbType.Int}
Dim lastEmploymentDate = New SqlParameter With {.ParameterName = "@LastEmploymentDate", .SqlDbType = SqlDbType.DateTime2}
'TODO: Write the query in full.
Dim sql = "INSERT INTO MasterStaffListTry (EENo,Name,Age,LastEmploymentDate)
VALUES
(@EENo, @Name, @Age, @LastEmploymentDate)"
Dim ci = New CultureInfo("en-US")
Using conn As New SqlConnection("yourConnectionStringGoesHere")
Using sqlcmd As New SqlCommand(sql, conn)
'TODO: Add all the parameters.
sqlcmd.Parameters.Add(eeNo)
sqlcmd.Parameters.Add(name)
sqlcmd.Parameters.Add(age)
sqlcmd.Parameters.Add(lastEmploymentDate)
Dim led As DateTime ' This will store the lastEmploymentDate when it has been parsed.
For Each r As DataRow In oleDt.Rows
'TODO: Use TryParse for all applicable data.
Dim ledOK = DateTime.TryParse(r(11).ToString(), ci, Nothing, led)
'TODO: Check all the parsing worked, e.g. If ledOK AndAlso variable2OK AndAlso variable3OK Then
If ledOK Then
eeNo.Value = r(0).ToString()
name.Value = r(1).ToString()
age.Value = Convert.ToInt32(r(2))
lastEmploymentDate.Value = led
sqlcmd.ExecuteNonQuery()
End If
Next
End Using
End Using
指定在嘗試決議日期或數字時要使用的區域性是一個好主意,例如 1/12/2020 可以是 1 月的第一天或 1 月 12 日。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/315670.html
標籤:sql-server 擅长 网络
