mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-19 12:30:32 +00:00
79 lines
2.1 KiB
C#
79 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Data.SQLite;
|
|
using System.Data;
|
|
|
|
namespace Cosmos.Debug.Common
|
|
{
|
|
public class SqliteBulkCopy: IDisposable
|
|
{
|
|
private bool mDisposed = false;
|
|
public void Dispose()
|
|
{
|
|
if (mDisposed)
|
|
{
|
|
return;
|
|
}
|
|
mDisposed = true;
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
private readonly SQLiteConnection mConnection;
|
|
public SqliteBulkCopy(SQLiteConnection connection)
|
|
{
|
|
mConnection = connection;
|
|
}
|
|
|
|
public string DestinationTableName
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public void WriteToServer(IDataReader reader)
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
// initialize bulk copy
|
|
|
|
using (var trans = mConnection.BeginTransaction())
|
|
{
|
|
using (var command = mConnection.CreateCommand())
|
|
{
|
|
var fieldNames ="";
|
|
var paramNames = "";
|
|
SQLiteParameter[] parms = new SQLiteParameter[reader.FieldCount];
|
|
for (int i = 0; i < reader.FieldCount; i++)
|
|
{
|
|
fieldNames += "\"" + reader.GetName(i) + "\",";
|
|
paramNames += "?,";
|
|
parms[i] = new SQLiteParameter();
|
|
command.Parameters.Add(parms[i]);
|
|
}
|
|
fieldNames = fieldNames.TrimEnd(',');
|
|
paramNames = paramNames.TrimEnd(',');
|
|
|
|
command.Transaction = trans;
|
|
command.CommandText = String.Format("insert into \"{0}\" ({1}) values ({2})", DestinationTableName, fieldNames, paramNames);
|
|
command.Prepare();
|
|
do
|
|
{
|
|
for (int i = 0; i < reader.FieldCount; i++)
|
|
{
|
|
var parm=parms[i];
|
|
if (parm != null)
|
|
{
|
|
parm.Value = reader.GetValue(i);
|
|
}
|
|
}
|
|
command.ExecuteNonQuery();
|
|
} while (reader.Read());
|
|
}
|
|
trans.Commit();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|