Import text information into the database using FSO

zhaozj2021-02-16  52

Import text information into the database using FSO

In developing web applications, we often need to process drives, folders, and files in the file system, such as collecting information about the drive; create, add, move, or delete folders and files, etc. A new set of FSO (File System Object) object models is available in VB6 to access the file system. This model provides an object-based tool that makes a variety of operations in the application through a series of properties and methods provided by it. 1. FSO Introduction The FSO object model contains the following objects: Drive Object: Allows the system physical or through the LAN and the system logical, the available space, shared name, etc. of the CD-ROM and other drives. Folder object: Allow creation, deleting, or moving folders, and querying the name, path of the folder, etc. to the system. Files object: Allow creation, delete, or moving files, and query the name, path of the file, etc. TEXTSTREAM object: Allows creation and reading and writing text files. FileSystemObject Object: Provides a single way for drives, folders, and file operations, which can be used as a collection of several objects above and often use them. Many methods associated with this object repeat the methods in the previous four objects, so we can do most of the drives, folders, and files through the FileSystemObject object, or through the corresponding drive, folder or file object. These components are operated. The FSO model implements the operation of the same object by two ways, and its operational effect is the same, and the purpose of providing this redundancy function is to achieve maximum programming flexibility.

In this article, we will explain the TEXTSTREAM objects that use the FSO object model to the operation of the text file.

(1) Use FileSystemObject to get text file objects 1. Creating a FileSystemObject object instance To perform a file action, you must first create a FileSystemObject object instance to create or open a file. Creating a FileSystemObject object instance The specific format is (in AfileSystemObject) as an example: set AfileSystemObject = CreateObject ("scripting.filesystemObject") 2. Using FileSystemObject to get text file objects TextStreamFileSystemObject provides two ways to get text file object TextStream, where creating files are cretextFile, used to open an existing file is OpenTextFile, two methods return results are a TextStream An example of an object, using this object to perform a specific operation of the file. (1) Creating a new file Creating a new file for the specific format (take AfileSystemObject as an example): AfileSystemObject.createtextFile (NewFileName, Overwriteexistingfile, isunicate) where: NewFileName is a string value, specify the name of the file to be created, usually a file The actual path add file name, such as c: /webshare/aspsamp/filetest.txt overwriteexistingfile is a boolean value, indicating whether or not to overwrite the original file if there is a file existence. This parameter can be omitted, and the default is false, ie the original file is not overwritten. Isunicode is a Boolean value that means that the file to be created is an ASCII file or a Unicode file. This parameter can be omitted, and the default is false, which is an ASCII file. (2) Open the existing file to open the specific format of the presented file, as an example of AfileSystemObject: AfileSystemObject.OpenTextFile (FileName, Iomode, Create, Format) where: FileName is a string value, specify the name of the file you want to open , Usually the actual path of the file is added, c: /filepath/test.txt omode is a constant value, indicating the purpose of opening the file, forreading (1) means for reading data; FORAPPpending means for increasing data. This parameter can be omitted, and it is forreading by default. Create is a Boolean value that indicates whether the file to be opened does not exist, the parameter can be omitted, and the default is false, that is, the new file is not created. Format indicates the way the file is opened. Its possible values ​​and meanings are as follows: Tristatetrue: Open in Unicode. Tristatefalse: Open in ASCII. TristateuseDefault: Open by the default manner. This parameter can be omitted, the default is TristateFalse, which is an ASCII mode. (two). File operation with TextStream After establishing or opening the file,

The actual operation of the file can be performed using the method provided by the object TextStream. 1. Methods for write operations include: (1) Write (string) Writes to the file by string specified by String. (2) WriteLine (string) writes the string specified by the string in the file and writes a wrap character. Parameters String can be omitted, and an empty line will be inserted in the file. (3) WriteBLANKLines (Numoflines) Inserts several spaces in the file, and the number of rows is specified by numoflines. 2. Methods and attribute methods for reading operations are: (1) ATENDOFLINE This property is a boolean value that indicates whether the file pointer has pointed to the current line. (2) ATENDOFSTREAM This property is a boolean value indicating whether the file pointer has pointed to the file. (3) COLUMN This property is an integer value indicating the location of the file pointer in the current row. ⑷ Line This property is an integer value indicating the line number of the row where the file pointer is located. ⑸ Read (NumOfcharacters) This method starts from the current location of the file, reads a string of characters specified by NumOfcharacters, returns a string. ⑹ Readline This method starts from the current location of the file, read into the content of the current row until the end, returns a string. ⑺ Readall This method starts from the current location, read the content of the entire file until the file ends, returns a string. ⑻ Skip (NumOfcharacters) This method starts from the current location of the file, skips several characters specified by the number of NumOfcharacters. ⑼ Skipline This method starts from the current location of the file and skip the content of the current line. 3. Methods for shutting down files are: (1) Close Close the file that has been established or opened. (3), below illustrate how to use FSO to read text files and how to save to the database:

1. Create a page that reads the file path: file.htm

...





...

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

<% @ Language = "VBScript"%>

<% Response.buffer = true%>

<%

StrConn = "DSN = DataSourceName"

Set conn = server.createObject ("adoDb.connection")

Conn.open straconn

Set objcomm = server.createObject ("adoDb.command") Objcomm.commandtext = "sp_addmsg" "call storage procedure

Objcomm.commandtype = adcmdstoredproc

Set objcomm.activeConnection = conn

'' '' '' '' 'Creating Input Output Parameters'' '' '' '' '' ''

Set objParameCom = Objcomm.createParameter (WC_ECompanyName ", Advarchar, Adparaminput, 100)

Objcomm.Parameters.Append Objparamecom

'@in_ecompanyname varchar (50), - Company English name

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

...

'' '' '' '' 'Creation parameters complete "' '' '' '' '' '' '' '' '' '

%>

<%

DIM AllText, Strline1, Strline2, Strline3

DIM STRPATH, FILEURL

FileURL = ""

StrPath = trim (Request.form ("Path"))

FileURL = STRPATH

SET FSO = CreateObject ("scripting.filesystemObject")

Set specialstream = fso.opentextfile (fileurl, 1, false, tristatefalse)

'''''''''Extract data''''''''''''''''''''''''''''

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

'' '' '' '' 'Adds the variable to the parameter set' '' '' '' '' '' ''. '

ObjparameCom.Value = strline1

Objparamccom.value = strline2

Objparamaddr.value = strline3

...

'' '' '' '' Operation end '' '' '' '' '' '' '' '' ''

Objcomm.execute () 'Run command

Loop

Response.write "
" "import library success! [Continue import]

Set conn = Nothing

SET FSO = Nothing

Set atextstream = Nothing

%>

Attachment: Storage Procedure sp_addmsg code

Create Procedure DBO.SP_ADDMSG - Import foreign business information

(

@IN_CompanyName Varchar (100), - Company Name

@IN_Address Varchar (200), - Company address

@IN_CITY VARCHAR (100), - Company Office

...

)

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 0Set NoCount OFF

At this point, the full text content is ended, and this article hopes that this article can bring a certain help to the reader.

转载请注明原文地址:https://www.9cbs.com/read-23241.html

New Post(0)