Use FSO to import text information to the database

Source: Internet
Author: User

Developing Web Applications Program . In VB6, a new object model named FSO (File System Object) is provided to access the file system. This model provides an object-based tool. Through a series of attributes and methods provided by it, we can perform various operations on the file system more easily and flexibly in the application.
I. FSO Introduction
The FSO object model contains the following objects:
Drive object: allows you to collect information about the available space, shared names, and other information of a drive, such as a hard disk, a CD-ROM, that is physically connected to the system through a LAN or a logical connection to the system.
Folder object: allows you to create, delete, or move folders, and query the folder name and path from the system.
Files object: allows you to create, delete, or move files, and query the file name and path from the system.
Textstream object: allows you to create and read/write text files.
FileSystemObject object: provides a complete set of methods for drive, folder, and file operations. It can be viewed as a collection of the above objects and is often used with them. Many methods associated with this object repeat the methods in the previous four objects. Therefore, we can perform most operations on the drive, folder, and file through the FileSystemObject object, you can also operate these components through the corresponding drive, folder or file object. The FSO model implements operations on the same object in two ways, with the same effect. The purpose of providing this redundancy function is to achieve maximum programming flexibility.
In this article Article , We will explain how to use the textstream object of the FSO object model to operate text files.

(1) Use FileSystemObject to obtain text file objects
1. Create a FileSystemObject object instance
To perform file operations, you must first create a FileSystemObject object instance to create or open a file. The specific format of creating a FileSystemObject object instance is (using afilesystemobject) as an example:
Set afilesystemobject = Createobject ("scripting. filesystemobjecct ")
2. Use FileSystemObject to obtain the textstream object of a text file.
FileSystemObject provides two methods to obtain the textstream object of a text file.
The object is createtextfile, which is used to open an existing object is opentextfile. Both methods return results
An instance of a textstream object, which can be used to perform file operations.
(1) create a new file
The specific format of the method for creating a new file is as follows (take afilesystemobject as an example ):
Afilesystemobject. createtextfile (newfilename, overwriteexistingfile, isunicode)
Where:
Newfilename is a string value that specifies the name of the file to be created, usually the actual path of the file
Add the file name, for example, C: \ webshare \ aspsamp \ filetest.txt.
Overwriteexistingfile is a Boolean value that indicates whether to overwrite a file with the same name if it exists.
The original file. This parameter can be omitted. The default value is false, that is, the original file is not overwritten.
Isunicode is a Boolean value, indicating whether the file to be created is an ASCII file or a Unicode file,
This parameter can be omitted. The default value is false, which is an ASCII file.
(2) open an existing file
The specific format of the method to open an existing file is (take afilesystemobject as an example ):
Afilesystemobject. opentextfile (filename, iomode, create, Format)
Where:
Filename is a string value that specifies the name of the file to be opened, usually the actual path of the file.
Add the file name, c: \ filepath \ test.txt
Iomode is a constant value, indicating the object to be opened. forreading (1) indicates reading data;
Forappending indicates adding data. This parameter can be omitted. The default value is forreading.
Create is a Boolean value, indicating whether to create a new file when the file to be opened does not exist,
This parameter can be omitted. The default value is false, that is, no new file is created.
Format indicates how the file is opened. Its Values and meanings are as follows:
Tristatetrue: open in Unicode mode.
Tristatefalse: open in ASCII format.
Tristateusedefault: enabled by default.
This parameter can be omitted. The default value is tristatefalse, which is the ASCII method.
(2). Use textstream for file operations
After creating or opening a file, you can use the method provided by the object textstream to perform actual operations on the file.
1. The methods used for write operations are:
(1) write (string)
Write the string specified by string to the file.
(2) writeline (string)
Write a string specified by string in the file, and write a line break character.
The string parameter can be omitted. An empty line is inserted in the file.
(3) writeblanklines (numoflines)
Insert several blank lines in the file. The number of lines is specified by numoflines.
2. The methods and attribute methods used for read operations include:
(1) atendofline
This attribute is a Boolean value, indicating whether the file pointer has pointed to the end of the row of the current row.
(2) atendofstream
This attribute is a Boolean value, indicating whether the file pointer has pointed to the end of the file.
(3) Column
This attribute is an integer that indicates the position of the file pointer in the current row.
(4) Line
This attribute is an integer that indicates the row number of the file pointer.
Repeated read (numofcharacters)
This method reads several characters specified by the number of numofcharacters from the current position of the file and returns
String.
⑹ Readline
This method starts from the current position of the file, reads the content of the current row until the end of the row, and returns a string.
⑺ Readall
This method reads the content of the entire file from the current position until the end of the file. A string is returned.
⑻ SKIP (numofcharacters)
This method skips several characters specified by the number of numofcharacters from the current position of the file.
⑼ Skipline
This method skips the content of the current row starting from the current position of the file.
3. Methods used to close files include:
(1) Close
Close a file that has been created or opened.

(3) The following example shows how to use FSO to read text files and how to save them to the database:

1. Create a page to read the file path: file.htm

...

<Form method = post action = "upfile. asp">

<Div align = "center"> <br>

<Br>

<Br>

<Br>

<Input type = "file" name = "path" size = "40">

<Input type = "Submit" name = "Dr" value = "import information">

</Div>

</Form>

...

2. Write and save the obtained text value to the database.Code: Upfile. asp

<% @ Language = "VBScript" %>

<% Response. Buffer = true %>

<! -- # Include file = "adovbs. Inc" -->

<%

Strconn = "DSN = datasourcename"

Set conn = server. Createobject ("ADODB. Connection ")

Conn. Open strconn

Set objcomm = server. Createobject ("ADODB. Command ")

Objcomm. commandtext = "sp_addmsg" 'Call the Stored Procedure

Objcomm. commandtype = adw.storedproc

Set objcomm. activeconnection = Conn

''''' ''' Create input and output parameters '''''''''''''''''

Set objparamecom = objcomm. createparameter ("maid", advarchar, adparaminput, 100)

Objcomm. Parameters. append objparamecom

'@ In_ecompanyname varchar (50), -- company name in English

Set objparamaddr = objcomm. createparameter ("wc_address", advarchar, adparaminput, 200)

Objcomm. Parameters. append objparamaddr

'@ In_address varchar (50), -- company address

Set objparamcity = objcomm. createparameter ("wc_city", advarchar, adparaminput, 100)

Objcomm. Parameters. append objparamcity

'@ In_city varchar (50), -- City

...

''''' ''' After a parameter is created ''''''''''''''''''''

%>

<%

Dim alltext, strline1, strline2, strline3

Dim strpath, fileurl

Fileurl = ""

Strpath = trim (request. Form ("path "))

Fileurl = strpath

Set FSO = Createobject ("scripting. FileSystemObject ")

Set atextstream = FSO. opentextfile (fileurl, 1, false, tristatefalse)

''' Data extraction ''''''''''''''''''''''''''''

Do while not atextstream. atendofstream

''''' Initialization variable ''''''''''''''''

Strline1 = ""

Strline2 = ""

Strline3 = ""

...

'''''''''''''''''''''''''''''''

Atextstream. skipline

Atextstream. Skip (11)

Strline1 = trim (atextstream. Readline)

Atextstream. Skip (11)

Strline2 = trim (atextstream. Readline)

Atextstream. Skip (5)

Strline3 = trim (atextstream. Readline)

...

'End if

''' Add the variable to the parameter set '''''''''''''

Objparamecom. value = strline1

Objparamccom. value = strline2

Objparamaddr. value = strline3

...

''' Operation termination ''''''''''''''''''''

Objcomm. Execute () 'Run the command

Loop

Response. Write "<br>" + "imported to the database! <A href=dolist.html> [Continue import] </a> <br>"

Set conn = nothing

Set FSO = nothing

Set atextstream = nothing

%>

Appendix: storage procedure sp_addmsg code

Create procedure DBO. sp_addmsg -- import foreign enterprise information

(

@ In_companyname varchar (100), -- company name

@ In_address varchar (200), -- company address

@ In_city varchar (100) -- the city where the company is located

...

)

As

Set nocount on

Begin tran

Insert into tb_wclibrary (

Wc_companyname,

Wc_ccompanyname,

Wc_address,

...

)

Values (

@ In_companyname,

@ In_ccompanyname,

@ In_address,

...

)

If @ error <> 0

Begin

Rollback tran

Return-1

End

Commit tran

Return 0

Set nocount off

At this point, the full text is over. I hope this article will help readers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.