1. 程式人生 > >excel 匯入到access 提示索引或主關鍵字不能為空 但是表中的主關鍵字列並不是空的

excel 匯入到access 提示索引或主關鍵字不能為空 但是表中的主關鍵字列並不是空的

excel匯入到access 提示索引或主關鍵字不能為空但是表中的主關鍵字列並不是空的,因為我的表中就只有一條記錄,錯誤排查了好久才弄好,原來是因為我把excel當成一個數據庫進行操作的時候,居然自動把末尾的空行也進行了匯入操作,空行的索引列當然為空了,所以一直提示這個問題!

解決辦法:從excelselect 的時候加上where索引 is not null

附加將excel 匯入到accessVB.net 程式碼

(下面的程式碼寫在module中,需要的地方再用以下語句進行呼叫: CallExportExcelSheetToAccess(ComboBox_sheet.SelectedItem.ToString, selexcel,ComboBox_sheet.SelectedItem.ToString, TextBox_access.Text)):

PublicSub ExportExcelSheetToAccess(ByVal sSheetName As String, ByVal sExcelPath AsString, ByVal sAccessTable As String, ByVal sAccessDBPath As String)
Dim xlsApp As New Excel.Application
Dim xlsBook As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
xlsBook = xlsApp.Workbooks.Open(sExcelPath)
xlsSheet = xlsBook.Worksheets(sSheetName)

Dimcols As Integer
cols = xlsSheet.UsedRange.Columns.Count
Dim colName() As String
ReDim colName(cols)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim con1 As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim Sql1 As String = "select * from [" & sSheetName &"$]"
con1.CursorLocation = ADODB.CursorLocationEnum.adUseClient
con1.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;Datasource=" & sExcelPath & ";Extended Properties='Excel8.0;HDR=Yes'"
con1.Open()
rs.Open(Sql1, con1, ADODB.CursorTypeEnum.adOpenDynamic,ADODB.LockTypeEnum.adLockBatchOptimistic)
With rs
For i1 As Integer = 0 To cols - 1
colName(i1) = rs(i1).Name.ToString
' MsgBox(colName(i1))
Next
End With
rs.Close()
con1.Close()

xlsBook.Close()
xlsApp.Quit()
xlsSheet = Nothing
xlsBook = Nothing
xlsApp = Nothing


Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;DataSource= " & sAccessDBPath & " ")
con.Open()
Dim j As Integer
Dim tablename As String
tablename = Form_face.ComboBox_daoru.SelectedItem.ToString.Substring(0,Form_face.ComboBox_daoru.SelectedItem.ToString.Length - 1)
Dim strs As String
If (Form_daoru.ComboBox_ssbs.SelectedItem.ToString.Equals("碩士錄取")) Then
strs = "sslq"
Else
strs = "bslq"
End If
tablename = tablename + strs
Dim adapter As OleDbDataAdapter
Dim ds As New DataSet
adapter = New OleDbDataAdapter("select * from MSysObjects Where name ='" & tablename & "' and type = 1 and flags =0", con)
adapter.Fill(ds)


If ds.Tables(0).Rows.Count = 0 Then
MsgBox("資料庫中不存在要匯入的表,是否新建一張表?")
Dim myTrans As OleDbTransaction
myTrans = con.BeginTransaction
Try
Dim sql As String = "CREATE TABLE " & tablename & " (" & colName(0) & " char (100) )"
Dim myCmd As New OleDb.OleDbCommand(sql, con)
myCmd.Transaction = myTrans
myCmd.ExecuteNonQuery()
Dim sql2 As String
For j = 1 To cols - 1
sql2 = "alter table " & tablename & " add " &colName(j) & " char (100) "
' MsgBox(colName(j))
Dim myCmd2 As New OleDb.OleDbCommand(sql2, con)
myCmd2.Transaction = myTrans
myCmd2.ExecuteNonQuery()
Next
Dim sql3 As String = "alter table " & tablename & " addconstraint PK_xuehao primary key (xuehao)"
Dim myCmd3 As New OleDb.OleDbCommand(sql3, con)
myCmd3.Transaction = myTrans
myCmd3.ExecuteNonQuery()

'MsgBox("下面進行匯入工作……")
Dim Command As New OleDbCommand("insert into " & tablename &" select * from [Excel 12.0;HDR=YES;DATABASE=" & sExcelPath &"].[" & sSheetName & "$] where xuehao is not null", con)
Command.Transaction = myTrans
Command.ExecuteNonQuery()
MsgBox("資料匯入成功!",vbInformation)
myTrans.Commit()
Catch ex As Exception
MsgBox(ex.Message + "匯入不成功! ")
Try
myTrans.Rollback()
Catch ex2 As Exception
MsgBox(ex2.Message)
End Try
Finally
con.Close()
xlsSheet = Nothing
xlsBook = Nothing
xlsApp = Nothing

EndTry


Else
MsgBox("此表已經存在,是否追加?")
Try
'MsgBox("下面進行匯入工作……")
Dim Command As New OleDbCommand("insert into " & tablename &" select * from [Excel 12.0;HDR=YES;DATABASE=" & sExcelPath &"].[" & sSheetName & "$] where xuehao is not null", con)
Command.ExecuteNonQuery()
MsgBox("資料匯入成功!",vbInformation)
con.Close()
Catch ex As Exception
MsgBox(ex.Message + "匯入不成功! ")
End Try


End If


End Sub