我正在尝试从 Excel 文件 > 数据表 > 数据集 > Db 数据表导入数据
”因为我真的不明白问题实际上是从哪里开始所以这是一个相当长的代码“
(错误行:Dim Date1 As DateTime = dtrow(1)
)
Dim dt1 As New DataTable
Dim dt2 As New DataTable
Dim firstrow As Boolean = True
For Each row As IXLRow In ws2.Rows.Skip(1)
If firstrow Then
For Each cell As IXLCell In row.Cells
dt1.Columns.Add(cell.Value.ToString)
Next
firstrow = False
End If
Exit For
Next
For Each row As IXLRow In ws2.Rows.Skip(2)
dt1.Rows.Add()
Dim i As Integer = 0
For Each cell As IXLCell In row.Cells()
dt1.Rows(dt1.Rows.Count - 1)(i) = cell.Value.ToString()
i += 1
Next
Next
'importing data from dt1 into db'
ds.Tables.Add(dt1)
da.TableMappings.Add("Table", "dbo.fingerprintslogs")
ds.Tables(0).TableName = "Table"
conn.Open()
Dim query = " Delete From Fingerprintlogs"
Dim cmd As New SqlCommand(query, conn)
da.DeleteCommand = cmd
da.DeleteCommand.ExecuteNonQuery()
DataGridView1.DataSource = ds.Tables(0)
For Each dtrow As DataRow In ds.Tables(0).Rows
Dim EmployeeID As Integer = dtrow(0)
Dim Date1 As DateTime = dtrow(1)
Dim WeekDay = dtrow(2)
Dim Firstcheckin As DateTime = dtrow(3)
Dim Lastcheckout As DateTime = dtrow(4)
Dim query1 = "INSERT INTO fingerprintslogs Values('" & EmployeeID & "','" & Date1 & "','" & WeekDay & "','" & Firstcheckin & "','" & Lastcheckout & "')"
Dim cmd1 As New SqlCommand(query1, conn)
cmd1.Parameters.Add("@EmloyeeID", SqlDbType.Int).Value = EmployeeID
cmd1.Parameters.Add("@Date1", SqlDbType.DateTime).Value = Date1
cmd1.Parameters.Add("@WeekDay", SqlDbType.NVarChar).Value = WeekDay
cmd1.Parameters.Add("@Firstcheckin", SqlDbType.DateTime).Value = Firstcheckin
cmd1.Parameters.Add("@Lastcheckout", SqlDbType.DateTime).Value = Lastcheckout
cmd1.ExecuteNonQuery()
da.InsertCommand = cmd1
da.Update(ds.Tables(0))
Next
conn.Close()
此处发生错误“从字符串“2024-04-15”转换为类型“日期”无效”:Dim Date1 As DateTime = dtrow(1)
For Each cell As IXLCell In row.Cells()
dt1.Rows(dt1.Rows.Count - 1)(i) = cell.Value.ToString()
i += 1
Next
Dim Date1 As Date = Date.ParseExact(dtrow(1).ToString(), "yyyy-MM-dd",
System.Globalization.DateTimeFormatInfo.InvariantInfo)