Advertisement
ASP_Volume2 Files/ File Controls/ Input/ Output #36062

transferring data

This VB code shows how you can use ADO and Microsoft Scripting Runtime Library to transfer data from a database table to a text file.

AI

AI சுருக்கம்: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

மூலக் குறியீடு
original-source
Private Sub Command1_Click()
' declare a constant
Const conType As Integer = 8209
'declare an object variable
Dim rs As ADODB.Recordset
' create a recordset object and
' assign it to the object variable
Set rs = New ADODB.Recordset
' execute the open method
With rs
.ActiveConnection = _
"provider=microsoft.jet.oledb.4.0;" _
& "data source = c:yourdb.mdb"
 .Source = "select * from yourtable"
 .Open
 End With
' declare a dynamic array
Dim str()
' execute the getrows method
str() = rs.GetRows
' we do not need the recordset object
Set rs = Nothing
' declare the object variables
Dim fso As FileSystemObject
Dim txtfile As TextStream
' create a filesystemobject
Set fso = _
CreateObject("Scripting.FileSystemObject")
' execute the createtextfile function
Set txtfile = _
fso.CreateTextFile("c:\testfile.txt", True)
' we do not need the object
Set fso = Nothing
' declare the variables that
' will hold the values from the array
Dim v, strv
' declare the variables
Dim j As Integer
Dim x As Integer
' loop through the array
For x = LBound(str, 2) To UBound(str, 2)
 For j = LBound(str, 1) To UBound(str, 1)
' dump a value from the array into
' the strv variable and do
' with it whatever you want
 strv = str(j, x)
' it is an example what you can do
 If VarType(strv) = conType Then
 strv = CStr(strv)
 strv = "Picture"
 ElseIf IsNull(strv) Then
 strv = "Null"
 ElseIf strv = "" Then
 strv = """"
 ElseIf j > 0 Then
 strv = "'" & strv & "'"
 End If
' build a text line
 v = v & strv & ", "
' check if the loop reached the end of a row
 If j = UBound(str, 1) Then
' since the whole record was dumped into
' the v variable, the text line can
' be written into the file
 txtfile.WriteLine v
' reset the variable
 v = ""
 End If
 Next j
 Next x
' close the object
txtfile.Close
End Sub
அசல் கருத்துகள் (3)
வேபேக் மெஷினிலிருந்து (Wayback Machine) மீட்டெடுக்கப்பட்டது