Oracle 5.0 Reference Manual page 1939

Table of Contents

Advertisement

to the server. The class concerned is MySqlBulkLoader. This class has various methods, the main
one being
to cause the specified file to be loaded to the server. Various parameters can be set to
load
control how the data file is processed. This is achieved through setting various properties of the class.
For example, the field separator used, such as comma or tab, can be specified, along with the record
terminator, such as newline.
The following code shows a simple example of using the
table needs to be created, in this case in the
CREATE TABLE Career (
Name VARCHAR(100) NOT NULL,
Age INTEGER,
Profession VARCHAR(200)
);
A simple tab-delimited data file is also created (it could use any other field delimiter such as comma):
Table Career in Test Database
Name
Age
Profession
Tony
47
Technical Writer
Ana
43
Nurse
Fred
21
IT Specialist
Simon
45
Hairy Biker
Note that with this test file the first three lines will need to be ignored, as they do not contain table data.
This can be achieved using the
used to populate the
Career
using System;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string connStr = "server=localhost;user=root;database=test;port=3306;password=******;";
MySqlConnection conn = new MySqlConnection(connStr);
MySqlBulkLoader bl = new MySqlBulkLoader(conn);
bl.TableName = "Career";
bl.FieldTerminator = "\t";
bl.LineTerminator = "\n";
bl.FileName = "c:/career_data.txt";
bl.NumberOfLinesToSkip = 3;
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
// Upload data from file
int count = bl.Load();
Console.WriteLine(count + " lines uploaded.");
string sql = "SELECT Name, Age, Profession FROM Career";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0] + " -- " + rdr[1] + " -- " + rdr[2]);
}
rdr.Close();
Connector/Net Programming
database:
test
NumberOfLinesToSkip
table in the
database:
test
1919
class. First an empty
MySqlBulkLoader
property. This file can then be loaded and

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents