我想将数据从SQL Server移动到sqlite。我所做的是首先将数据从SQL Server移动到dataset
,然后从那里移动到sqlite表。
以下是执行此操作的代码。我相信可能会有更有效的方式
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);
}
先感谢您
尝试将所有插入包装到一个事务中:
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);
}
我使用sqlite遇到了插入速度相同的问题,这种技术解决了问题 - 插入速度大大增加。我不知道是否还有其他选择可以解决问题,如果这个问题不适合。