SqlLite用SQLiteTransaction快速导入数据

mysql与sql server都有整表导入的类库,但是查遍了资料发现sqlLite没有,除非自己去写个,发现用SQLiteTransaction导入数据也很快,附上代码

 /// <summary>
        /// 导入major表数据
        /// </summary>
        /// <param name="con"></param>
        /// <param name="dt"></param>
        public static void InsertMajor(string con, DataTable dt)
        {
            try
            {
                SQLiteConnection connection = new SQLiteConnection("data source=" + con);
                connection.Open();
                SQLiteCommand command = connection.CreateCommand();
                SQLiteTransaction transaction = connection.BeginTransaction();

                #region Sql语句
                command.CommandText = "INSERT OR IGNORE INTO major " +
                                                       "(id," +
                                                       "year," +
                                                       "major," +
                                                       "inter_code," +
                                                       "edu_department," +
                                                       "report_school ," +
                                                       "subject," +
                                                       "prm_level ," +
                                                       "city," +
                                                       "type," +
                                                       "level," +
                                                       "prm_major," +
                                                       "prm_type," +
                                                       "major_new)" +

                                                       "VALUES" +
                                                        "(" +

                                                       "@id," +
                                                       "@year," +
                                                       "@major," +
                                                       "@inter_code," +
                                                       "@edu_department," +
                                                       "@report_school ," +
                                                       "@subject," +
                                                       "@prm_level," +
                                                       "@city," +
                                                       "@type," +
                                                       "@level," +
                                                       "@prm_major," +
                                                       "@prm_type," +
                                                       "@major_new)";

                #endregion

                #region
                command.Parameters.AddWithValue("@id", "");
                command.Parameters.AddWithValue("@year", "");
                command.Parameters.AddWithValue("@major", "");
                command.Parameters.AddWithValue("@inter_code", "");
                command.Parameters.AddWithValue("@edu_department", "");
                command.Parameters.AddWithValue("@report_school", "");
                command.Parameters.AddWithValue("@subject", "");
                command.Parameters.AddWithValue("@prm_level", "");
                command.Parameters.AddWithValue("@city", "");
                command.Parameters.AddWithValue("@type", "");
                command.Parameters.AddWithValue("@level", "");
                command.Parameters.AddWithValue("@prm_major", "");
                command.Parameters.AddWithValue("@prm_type", "");
                command.Parameters.AddWithValue("@major_new", "");

                #endregion
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    command.Parameters["@id"].Value = dt.Rows[i]["id"].ToString();
                    command.Parameters["@year"].Value = dt.Rows[i]["year"].ToString();
                    command.Parameters["@major"].Value = dt.Rows[i]["major"].ToString();
                    command.Parameters["@inter_code"].Value = dt.Rows[i]["inter_code"].ToString();
                    command.Parameters["@edu_department"].Value = dt.Rows[i]["edu_department"].ToString();
                    command.Parameters["@report_school"].Value = dt.Rows[i]["report_school"].ToString();
                    command.Parameters["@subject"].Value = dt.Rows[i]["subject"].ToString();
                    command.Parameters["@prm_level"].Value = dt.Rows[i]["prm_level"].ToString();
                    command.Parameters["@city"].Value = dt.Rows[i]["city"].ToString();
                    command.Parameters["@type"].Value = dt.Rows[i]["type"].ToString();
                    command.Parameters["@level"].Value = dt.Rows[i]["level"].ToString();
                    command.Parameters["@prm_major"].Value = dt.Rows[i]["prm_major"].ToString();
                    command.Parameters["@prm_type"].Value = dt.Rows[i]["prm_type"].ToString();
                    command.Parameters["@major_new"].Value = dt.Rows[i]["major_new"].ToString();
                    command.ExecuteNonQuery();
                }

                transaction.Commit();
                command.Dispose();
                connection.Dispose();
            }
            catch (Exception ex)
            {
                string str = ex.ToString();
            }
        }

 

posted @ 2020-03-19 23:06  be--yourself  阅读(542)  评论(0编辑  收藏  举报