如何把数据库里的一个表的一个列,按我所需要的条件,导到一个文本文件里,比如说数据库datebase里的表biao1的格式是这样的:
编号 名称 有效否
001 KK 有
002 WW 无
我想把biao1中条件为有效的导到文本文件里,导进去的格式为:
导入当天的日期,编号,数量
2003-12-04,001 ,1程序里面该怎么做呢?
给大家鞠躬了 :)
编号 名称 有效否
001 KK 有
002 WW 无
我想把biao1中条件为有效的导到文本文件里,导进去的格式为:
导入当天的日期,编号,数量
2003-12-04,001 ,1程序里面该怎么做呢?
给大家鞠躬了 :)
On Error GoTo errhandler
If filename <> "" Then
Open filename For Input As #1
Do While Not EOF(1)
Input #1, col1, col2, col3 '代表3个字段
If i > 0 Then
'添加到数据库的过程
mrs.AddNew
If Len(col1) > 10 Then
MsgBox "学号太长,限定10个字符。", vbOKOnly + vbExclamation, "警告"
Exit Sub
Else
mrs!学号 = col1
End If
If Len(col2) > 10 Then
MsgBox "姓名太长,限定10个字符。", vbOKOnly + vbExclamation, "警告"
Exit Sub
Else
mrs!姓名 = col2
End If
If Len(col3) > 10 Then
MsgBox "班级名太长,限定10个字符。", vbOKOnly + vbExclamation, "警告"
Exit Sub
Else
mrs!班级 = col3
End If
mrs.Update
End If
i = i + 1
Loop
Close #1
mrs.Close
Set mrs = Nothing
MsgBox "数据已经导入到数据库", vbInformation, "导入"
Else
MsgBox "请选择数据文本。", vbOKOnly + vbExclamation, "警告"
End If
exithere:
Exit Sub
errhandler:
MsgBox Err.Description, vbOKOnly + vbExclamation, "警告"
Resume exithere
End Sub
Dim conn As ADODB.Connection
Dim rs As ADODB.RecordsetPrivate Sub cmdExport_Click()
Dim fnum As Integer
Dim file_name As String
Dim num_fields As Integer
Dim field_width() As Integer
Dim field_value As String
Dim i As Integer
Dim num_processed As IntegerOn Error GoTo MiscError'打开输出文件
fnum = FreeFile
file_name = App.Path & "\books.txt"
Open file_name For Output As fnum Set rs = New ADODB.Recordset
rs.Open "SELECT EmployeeID,LastName,FirstName,Title,TitleOfCourtesy,BirthDate,HireDate FROM employees ", conn, adOpenDynamic, adLockPessimistic num_fields = rs.Fields.Count
ReDim field_width(0 To num_fields - 1)
For i = 0 To num_fields - 1
field_width(i) = rs.Fields(i).DefinedSize
If field_width(i) < Len(rs.Fields(i).Name) Then
field_width(i) = Len(rs.Fields(i).Name)
End If
field_width(i) = field_width(i) + 1
Print #fnum, rs.Fields(i).Name;
Print #fnum, Space(field_width(i) - Len(rs.Fields(i).Name));
Next i
Print #fnum, "" Do While Not rs.EOF
num_processed = num_processed + 1
For i = 0 To num_fields - 1
field_value = rs.Fields(i).Value & ""
Print #fnum, field_value & Space(field_width(i) - Len(field_value));
Next i
Print #fnum, ""
rs.MoveNext
Loop rs.Close
Close fnum
MsgBox "成功导出了 " & Format(num_processed) & " 条记录."
conn.Close
Exit SubMiscError:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
End Sub
Private Sub Form_Load()
Set conn = New ADODB.Connection
conn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Northwind;Data Source=yang"
End Sub
你给我的例子是有关ADO的连接数据库方式
那如果是rdoConnection的应该怎么做呢?
谢谢你:)我刚学VB不久,不是很懂
private rs as rdoresultsetset conn=rdoenvironments(0).openconnection("pubs")'odbs数据源
set rs=conn.openresultset(ssql,rdopenkeyset,rdconcurvaluses)
rs("column0")
rs!column0