Read text files into the database using the BULK INSERT statement

xiaoxiao2021-03-06  74

SQL Server has a BCP tool for reading data from a text file, writes into the database, there is a similar BULK INSERT statement in T-SQL, which can achieve the same feature, the following function reads the content in a logfile into the webd table:

Public void importlogfile (String logfile)

{

OLEDBCOMMAND CMD = New OLEDBCommand ();

cmd.connection = CN; // CN is a database connection

// use bulk insert Statement

String ssql = "bulk insert Webd";

SSQL = "from '" logfile.replace ("'", "'') " '";

SSQL = "with"; "

SSQL = "DataFiletype = 'char'";

SSQL = ", FIELDTERMINATOR = ','"

SSQL = ", Rowterminator = '/ n'";

SSQL = ")";

// Run the bulk insert

cmd.commandtext = ssql;

cmd.executenonquery ();

}

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

New Post(0)