将MSFlexGrid中的数据导入到sql中,并判断重复的数据不导入。(已解决,见10楼)
如题,初学者,不太会定义和使用MSFlexGrid的movefirst和movenext方法,请大家帮忙看看我这样写的对不对?不行的话应该怎么修改?请大家指点一下。Private Sub Command4_Click()
Dim cnn As New ADODB.Connection
Dim rsTemp As New ADODB.Recordset
Dim rs1 As New ADODB.Recordset
Dim Djls As Integer
Dim InserStr As String
Dim i As Long
Djls = 0
'打开目标数据库的连接,准备导入数据
cnn.Open "Provider=SQLOLEDB.1;User ID=sa;pwd=" y69m10d17 ";Initial Catalog=Northwind;Data Source=" 192.168.1.38 ""
'main是后台数据库的表
' rs1.Open "main", cn2, adOpenDynamic, adLockBatchOptimistic, adCmdTable
'循环插入数据
If Recordset.RecordCount > 0 Then
Recordset.MoveFirst '我不知道怎么来定义和使用flexGrid的指针,这样写对不对?
With MSFlexGrid1
.Row = 1
While Not Recordset.EOF
If .TextMatrix(i, 3) = "JF" then
For i = 1 To .Rows - 1
If isnull (Recordset("CODE"))= True then
Msgbox("CODE不能为空,请检查第『"+CStr(Djls+1)+"』行记录")
EXIT sub
End if
If isnull (Recordset("时间"))= True then
Msgbox("时间不能为空,请检查第『"+CStr(Djls+1)+"』行记录")
EXIT sub
End if
rsTemp.Open "select top 1 * from main where trim(tmdata)="trim(Recordset("CODE")",cnn, adOpenDynamic, adLockReadOnly
'判断记录没有重复则插入表中
If rsTemp.EOF Then
InserStr = "insert into main(tmdata,date) Values ( '" + trim_(Recordset("CODE") + "'" + ",'" + trim(Recordset("时间")+ "')"
cnn.Execute (InserStr)
End If
Recordset.MoveNext
rsTemp.Close
Next i
End If
End With
Wend
End If
cnn.Close
Total_J1s.caption="共有『"+CStr(Djls)+"』记录数被导入"
End Sub
[此贴子已经被作者于2007-9-17 12:58:40编辑过]