【发布时间】:2017-12-20 21:03:56
【问题描述】:
我想将数据从 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);
}
提前谢谢你
【问题讨论】:
-
大约有 2000 条记录可供下载。从 sql server 数据库复制到数据集需要几秒钟,但将它们从数据集移动到 sqlite 表需要大约 20 分钟
-
表的结构是否相同?什么是存储过程代码?
-
是的,这些表具有相同的结构。下面是 sql server 中的存储过程,它选择下载到数据集的数据。在哪里可以再次添加代码?
-
这是一个简单的选择语句,如下所示:SELECT AoLId, AreaOfLawTitle From AreaofLaw WHERE AoLId > @AoLId
-
您要插入某些行或全部
标签: c# sql-server sqlite