优化此 C# 代码的其他方法
本文关键字:其他 方法 代码 优化 | 更新日期: 2023-09-27 18:34:29
我创建了 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();
}
}
}
}
谢谢
SqlBulkCopy 类帮助了我一次。链接在这里
我看到两个主要问题。首先,您应该在循环开始时打开StreamReader
,并在完成后关闭它。喜欢这个:
using (StreamWriter sw = new StreamWriter(sFilename, false, Encoding.UTF8)
{
while (dr.Read())
{
// convert here
// Each line is written to the StreamWriter
}
}
using
结构将确保StreamWriter
得到妥善关闭和处置。
第二个问题是将内容附加到字符串。不要附加字符串,而是使用 StringBuilder
:
StringBuilder s_fields = new StringBuilder();
StringBuilder s_values = new StringBuilder();
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.AppendFormat("{0},", _Arr[i_a].ToString());
s_values.AppendFormat("'{0}',", _dr[i_a].ToString());
}
else
{
s_fields.Append(_Arr[i_a].ToString());
s_values.AppendFormat("'{0}'", _dr[i_a].ToString());
}
}
strSQL = String.Format("INSERT INTO {0} ({1}) VALUES ({2})",
sTableName, s_fields.ToString(), s_values.ToString());
sw.WriteLine(strSQL);
}
这应该会大大提高您的表现。
我认为你的主要罪魁祸首是new StreamWriter(sFileName, true, Encoding.UTF8)
行代码。对于每个数据行,您正在打开文件,添加单个命令,然后关闭文件。那将非常低效。
尝试使用 StringBuilder
而不是写入流,然后在读取循环之外,将使用StringBuilder
生成的字符串仅写入文件一次。
我想你会发现这大大加快了速度。