Merging two SQLite database files (C# .NET)
I'm using C#/.NET with the C# wrapper for SQLite. I'm attempting to merge two SQLite databases together while excluding duplicates.
I found this, which is referenced from a few different forum questions. http://old.nabble.com/Attempting-to-merge-large-databases-td18131366.html
I've tried the below queries, which I structured from the link I provided, but they result in exceptions, the databases are not merged at all, and the original database is not changed whatsoever.
attach 'c:\test\b.db3' as toMerge;
insert into AuditRecords select * from toMerge.AuditRecords;
Here is my query code.
public void importData(String fileLoc)
{
SQLiteTransaction trans;
string SQL = "ATTACH '" + fileLoc + "' AS TOMERGE";
SQLiteCommand cmd = new SQLiteCommand(SQL);
cmd.Connection = connection;
connection.Open();
trans = connection.BeginTransaction();
int retval = 0;
try
{
retval = cmd.ExecuteNonQuery();
}
catch (Exception)
{
trans.Rollback();
MessageBox.Show("An error occurred, your import was not completed.");
}
finally
{
trans.Commit();
cmd.Dispose();
connection.Close();
}
SQL = "INSERT INTO SUBCONTRACTOR SELECT * FROM TOMERGE.SUBCONTRACTOR";
cmd = new SQLiteCommand(SQL);
cmd.Connection = connection;
connection.Open();
t开发者_Go百科rans = connection.BeginTransaction();
retval = 0;
try
{
retval = cmd.ExecuteNonQuery();
}
catch (Exception)
{
trans.Rollback();
MessageBox.Show("An error occurred, your import was not completed.");
}
finally
{
trans.Commit();
cmd.Dispose();
connection.Close();
}
}
My question is, what am I doing wrong? And is anyone familiar with the insert command? I'm unsure if it will exclude duplicates as I need.
When you attach a database in SQLite, you need execute every statement (whether insert, update, delete) in a single Connection/Transaction. Dont Close the Connection in between. It should complete in a single Transaction.
try this
public void importData(String fileLoc)
{
string SQL = "ATTACH '" + fileLoc + "' AS TOMERGE";
SQLiteCommand cmd = new SQLiteCommand(SQL);
cmd.Connection = connection;
connection.Open();
int retval = 0;
try
{
retval = cmd.ExecuteNonQuery();
}
catch (Exception)
{
MessageBox.Show("An error occurred, your import was not completed.");
}
finally
{
cmd.Dispose();
}
SQL = "INSERT INTO SUBCONTRACTOR SELECT * FROM TOMERGE.SUBCONTRACTOR";
cmd = new SQLiteCommand(SQL);
cmd.Connection = connection;
retval = 0;
try
{
retval = cmd.ExecuteNonQuery();
}
catch (Exception)
{
MessageBox.Show("An error occurred, your import was not completed.");
}
finally
{
cmd.Dispose();
connection.Close();
}
}
精彩评论