【问题标题】:Inserting Data from SQL Server to Sqlite将数据从 SQL Server 插入到 Sqlite
【发布时间】: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


【解决方案1】:

尝试将所有插入包装到一个事务中:

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 的插入速度方面遇到了同样的问题,这种技术解决了这个问题 - 插入速度大大提高了。如果这个不合适,我不知道是否有任何其他选项可以解决这个问题。

【讨论】:

  • 为什么会提高性能而不是导致延迟?
  • @Panagiotis Kanavos,因为 sqlite 在显式事务下的插入速度要快得多。快多了。我自己处理过它并遇到了同样的麻烦 - 它工作得太慢了,在将它全部包装到事务中之后它变得闪电般快速。
  • 然后在答案中解释它。我还怀疑当您使用 WAL 时事务会提高性能。即便如此,20 分钟记录 2000 条记录还是太慢了。
  • 我要感谢所有为解决这个问题做出贡献的人。解决方案是我的好朋友提供的。用 SQLiteTransaction 包装插入。它像魔术一样工作。在不到 10 秒的时间内记录超过 10,000 条记录。我的意思是不到10秒。完成同样的数字花了 30 多分钟。伙计们,我深深地感谢你们。
猜你喜欢
  • 1970-01-01
  • 2022-01-18
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 2018-01-10
相关资源
最近更新 更多