我创建了 mdb 记录到 sql 记录的转换,对我来说它工作正常.. 但是当记录更大时速度很慢.. 我认为还有其他方法可以优化记录的解析和提取?
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace conMDBToSQL
{
class Program
{
static void Main()
{
// string sFileName = "C:\\sqlrecords.mdb";
string sFileName = "C:\\sample.mdb";
OleDbConnection _con = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFileName + ";Mode=Share Exclusive;User Id=admin;Password=;" );
try
{
_con.Open();
string sTableName = "users";
string strSQL = String.Empty;
strSQL = "SELECT * FROM " + sTableName + " ORDER BY 1";
OleDbCommand _cmd = new OleDbCommand( strSQL, _con );
_cmd.CommandType = CommandType.Text;
OleDbDataReader _dr = _cmd.ExecuteReader( CommandBehavior.SchemaOnly );
ArrayList _Arr = new ArrayList();
DataTable dt = _dr.GetSchemaTable();
foreach ( DataRow drow in dt.Rows )
{
// Console.Write( drow[0].ToString() + "\n" );
_Arr.Add( drow[0].ToString() );
}
_dr.Close();
_cmd.Dispose();
strSQL = "SELECT * FROM " + sTableName + " ORDER BY 1";
_cmd = new OleDbCommand(strSQL, _con);
_cmd.CommandType = CommandType.Text;
_dr = _cmd.ExecuteReader();
string s_fields = String.Empty;
string s_values = String.Empty;
int arr_count = _Arr.Count - 1;
while (_dr.Read())
{
for(int i_a = 0; i_a <= arr_count;i_a++)
{
if (i_a <= arr_count)
{
s_fields += _Arr[i_a].ToString() + ",";
s_values += "'" + _dr[i_a].ToString() + "',";
}
else
{
s_fields += _Arr[i_a].ToString();
s_values += "'" + _dr[i_a].ToString() + "'";
}
}
// Build, query
strSQL = String.Empty;
strSQL = "INSERT INTO " + sTableName + "("+ s_fields +") VALUES ("+ s_values +")";
// Write, text file
StreamWriter sw = new StreamWriter(sFileName, true, Encoding.UTF8);
sw.Write(strSQL);
sw.Close();
sw = null;
}
Console.WriteLine("\n");
Console.WriteLine("Finished..");
Console.ReadKey();
}
catch ( OleDbException olex )
{
throw olex;
}
finally
{
_con.Close();
}
}
}
}
谢谢,