在VB中,把excel导入到SQL数据库中,请高手帮忙~
PrivateSubCommand11_Click()CommonDialog1.FileName=""CommonDialog1.Flags=4096CommonDia...
Private Sub Command11_Click()
CommonDialog1.FileName = ""
CommonDialog1.Flags = 4096
CommonDialog1.Filter = "(*.xls)"
CommonDialog1.FilterIndex = 3
CommonDialog1.DialogTitle = "Open File(*.xls)"
CommonDialog1.Action = 1
If CommonDialog1.FileName = "" Then
MsgBox "no file selectd", 37, "checking"
Else
Dim Cn As ADODB.Connection
Dim strSQL As String
Dim lngRecsAff As Long
Set Cn = New ADODB.Connection
Cn.Open "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=学生宿舍管理系统"
strSQL = "INSERT INTO 学生基本信息 select " & _
"姓名,性别,民族,学号,政治面貌,系别,区队,户籍地,联系方式,宿舍楼号,寝室号,床号,备注 FROM " & _
"OPENROWSET('Microsoft.Jet.OLEDB.4.0'," & _
"'Excel 8.0;Database=" & CommonDialog1.FileName & "',Sheet1$)"
Debug.Print strSQL
Cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
Debug.Print "Records affected: " & lngRecsAff
MsgBox "本次共导入" & lngRecsAff & "条记录!"
Cn.Close
Set Cn = Nothing
End If
End Sub
以上是可以实现将EXCEL导入SQL中,但是缺点是也可以导入重复的信息,所以我想怎么让它能检测重复的信息,只导入不重复的,我想加一句
Where 学号 <> Adodc1.Recordset.Fields("学号"),但是提示说.Fields未定义,请问这是怎么回事?还有其他办法吗?
回答好了+分!!!谢谢 展开
CommonDialog1.FileName = ""
CommonDialog1.Flags = 4096
CommonDialog1.Filter = "(*.xls)"
CommonDialog1.FilterIndex = 3
CommonDialog1.DialogTitle = "Open File(*.xls)"
CommonDialog1.Action = 1
If CommonDialog1.FileName = "" Then
MsgBox "no file selectd", 37, "checking"
Else
Dim Cn As ADODB.Connection
Dim strSQL As String
Dim lngRecsAff As Long
Set Cn = New ADODB.Connection
Cn.Open "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=学生宿舍管理系统"
strSQL = "INSERT INTO 学生基本信息 select " & _
"姓名,性别,民族,学号,政治面貌,系别,区队,户籍地,联系方式,宿舍楼号,寝室号,床号,备注 FROM " & _
"OPENROWSET('Microsoft.Jet.OLEDB.4.0'," & _
"'Excel 8.0;Database=" & CommonDialog1.FileName & "',Sheet1$)"
Debug.Print strSQL
Cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
Debug.Print "Records affected: " & lngRecsAff
MsgBox "本次共导入" & lngRecsAff & "条记录!"
Cn.Close
Set Cn = Nothing
End If
End Sub
以上是可以实现将EXCEL导入SQL中,但是缺点是也可以导入重复的信息,所以我想怎么让它能检测重复的信息,只导入不重复的,我想加一句
Where 学号 <> Adodc1.Recordset.Fields("学号"),但是提示说.Fields未定义,请问这是怎么回事?还有其他办法吗?
回答好了+分!!!谢谢 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询