问题
I want to move data from SQL Server to sqlite. What I do is first move the data from SQL Server to a dataset
, and then move from there to the sqlite table.
The following is the code that does that. I believe there may be more efficient way
try
{
using (SqlConnection sqlConnection = new SqlConnection(DataSources.RemoteConnectionString()))
{
sqlConnection.Open();
using (SqlCommand sqlCommand = new SqlCommand("[DB7934_businessmind].[DownloadAreaOfLaw]", sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@AoLId", aolid);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(aolDataSet.TempAoL);
if (aolDataSet.TempAoL.Rows.Count > 0)
{
using (SQLiteConnection sqliteConnection = new SQLiteConnection(DataSources.LocalConnectionString()))
{
sqliteConnection.Open();
using (SQLiteCommand sqliteCommand = new SQLiteCommand(sqliteConnection))
{
foreach (Models.AoLDataSet.TempAoLRow r in aolDataSet.TempAoL.Rows)
{
sqliteCommand.CommandText = "INSERT INTO AreaOfLaw(AoLId, AreaOfLawTitle) VALUES(@AoLId, @AreaOfLawText)";
sqliteCommand.Parameters.AddWithValue("@AoLId", r.AoLId);
sqliteCommand.Parameters.AddWithValue("AreaOfLawText", r.AreaOfLawTitle);
sqliteCommand.ExecuteNonQuery();
}
sqliteCommand.Dispose();
}
sqliteConnection.Close();
}
}
}
sqlConnection.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("An error has occurred while downloading Areas Of Law from the cloud, the original error is: " + ex.Message, "Area Of Law", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Thank you in advance
回答1:
Try wrapping all inserts into one transaction:
try
{
using (SqlConnection sqlConnection = new SqlConnection(DataSources.RemoteConnectionString()))
{
sqlConnection.Open();
using (SqlCommand sqlCommand = new SqlCommand("[DB7934_businessmind].[DownloadAreaOfLaw]", sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@AoLId", aolid);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(aolDataSet.TempAoL);
if (aolDataSet.TempAoL.Rows.Count > 0)
{
using (SQLiteConnection sqliteConnection = new SQLiteConnection(DataSources.LocalConnectionString()))
{
sqliteConnection.Open();
using(SqliteTransaction tr = sqliteConnection.BeginTransaction())
{
using (SQLiteCommand sqliteCommand = new SQLiteCommand(sqliteConnection))
{
foreach (Models.AoLDataSet.TempAoLRow r in aolDataSet.TempAoL.Rows)
{
sqliteCommand.CommandText = "INSERT INTO AreaOfLaw(AoLId, AreaOfLawTitle) VALUES(@AoLId, @AreaOfLawText)";
sqliteCommand.Parameters.AddWithValue("@AoLId", r.AoLId);
sqliteCommand.Parameters.AddWithValue("AreaOfLawText", r.AreaOfLawTitle);
sqliteCommand.ExecuteNonQuery();
}
sqliteCommand.Dispose();
}
tr.Commit();
}
sqliteConnection.Close();
}
}
}
sqlConnection.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("An error has occurred while downloading Areas Of Law from the cloud, the original error is: " + ex.Message, "Area Of Law", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
I've came across the same issue with insertion speed using sqlite and this technique solved the problem - insertion speed increased tremendously. I don't know if there are any other options available to beat the problem if this one doesn't fit.
来源:https://stackoverflow.com/questions/47904815/inserting-data-from-sql-server-to-sqlite