码迷,mamicode.com
首页 > 数据库 > 详细

SqlLite用SQLiteTransaction快速导入数据

时间:2020-03-19 23:16:42      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:类库   ati   mys   summary   school   for   tab   parameter   nsa   

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();
            }
        }

 

SqlLite用SQLiteTransaction快速导入数据

标签:类库   ati   mys   summary   school   for   tab   parameter   nsa   

原文地址:https://www.cnblogs.com/CityOfThousandFires/p/12528026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!