我正在将一个 excel 表导入我的 sql 数据库。一切正常,但我想将列标题作为静态值读取。不知道该怎么做。这是我用来阅读表格的代码:
'Connection String to Excel Workbook
Dim excelConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & MyFile & ";Extended Properties=""Excel 12.0;HDR=Yes;"""
' Create Connection to Excel Workbook
Using connection As New System.Data.OleDb.OleDbConnection(excelConnectionString)
'List columns you need from the Excel file
Dim command As New System.Data.OleDb.OleDbCommand("Select * FROM [" & txtSheetName.Text & "$]", connection)
connection.Open()
' Create DbDataReader to Data Worksheet
Using dr As System.Data.OleDb.OleDbDataReader = command.ExecuteReader()
Dim strSql As String = ""
strSql = "INSERT INTO tblMine (" & _
"Header1, Field1, Field2 " & _
") VALUES (" & _
"Header1, Field1, Field2)"
Try
If dr.HasRows() Then
While dr.Read()
If Convert.ToString(dr.Item(1)) <> "" Then
Dim cmd As New SqlClient.SqlCommand
cmd.Connection = cn
cmd.CommandType = CommandType.Text
cmd.CommandText = strSql
'DataTable1.Columns[0].ColumnName
cmd.Parameters.Add("@Header1", SqlDbType.VarChar).Value = ???
cmd.Parameters.Add("@Field1", SqlDbType.VarChar).Value = Convert.ToString(dr.Item(0))
cmd.Parameters.Add("@Field2", SqlDbType.Int).Value = Convert.ToInt32(dr.Item(1))
cmd.ExecuteScalar()
End If
End While
End If