求vb简单小程序,谢谢

2024-11-02 16:33:08
推荐回答(2个)
回答1:

先在“工程-引用”中选上“Microsoft ActiveX Data Object 2.8 Library”
然后把下列代码加入到窗体代码中:
Private Sub Command1_Click()
Dim lSuccess As Long
Dim sPath As String
Dim lFail As Long
Dim lFailLine() As Long
Dim sFailText() As String
Dim conn As ADODB.Connection
Dim rs As Recordset
Dim lAffected As Long
sPath = App.Path
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"

Set conn = New Connection
'连接SQL Server数据库
'conn.ConnectionString = "driver={SQL server};server=[SQL服务器计算机名]; uid=[uid];pwd=[密码];database=[数据库名]"
'连接Access数据库
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sPath & "db.mdb;" & " Persist Security Info=False"

conn.Open

lSuccess = 0
lFail = 0
ReDim lFailLine(0) As Long
ReDim sFailText(0) As String
Open sPath & "test.txt" For Input As #1 '把test.txt换成你自己的文本文件名
While Not EOF(1)
Line Input #1, strtmp
str1 = Split(strtmp, vbTab)
On Error Resume Next
conn.Execute "insert into table1(a,b,c) values('" & str1(0) & "','" & str1(1) & "','" & str1(2) & "')", lAffected
On Error GoTo 0
If lAffected > 0 Then
lSuccess = lSuccess + 1
Else
ReDim Preserve lFailLine(0 To lFail) As Long
ReDim Preserve sFailText(0 To lFail) As String
lFailLine(lFail) = lSuccess + lFail + 1
sFailText(lFail) = strtmp
lFail = lFail + 1
End If
Wend
Close #1

conn.Close
Set conn = Nothing

Dim sMsg As String, i As Long
sMsg = "执行完毕!" & vbCrLf & "成功插入" & lSuccess & "行记录到数据库中。"
If lFail > 0 Then
sMsg = sMsg & vbCrLf & "其中有" & lFail & "行因数据格式不符合规范,插入失败,详见" & sPath & "Report.txt"
Open sPath & "report.txt" For Output As #1
For i = 0 To lFail - 1
Print #1, "行号:" & lFailLine(i) & vbTab & sFailText(i)
Next
Close #1
End If
MsgBox sMsg
End Sub

回答2:

insert into [表名](a,b,c) values(123,dfh,33dg);
insert into [表名](a,b,c) values(234,ftg,44fg);

这是最基本的sql语句,不懂的话到网上下载sql手册常常查查