Inserting Data from SQL Server to Sqlite

元气小坏坏 提交于 2021-01-28 14:23:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!