SqlLite用SQLiteTransaction快速导入数据

Posted 自制力是强者的本能

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
            }
        }

 

以上是关于SqlLite用SQLiteTransaction快速导入数据的主要内容,如果未能解决你的问题,请参考以下文章

node安装sqllite3

用sqlliteStudio 连接安卓sqllite的时候选择哪个数据库都提示这样一个错误

如何sqllite数据库复制到android手机上

sqllite to mysql 工具 sqllite 转 mysql

sqllite to mysql 工具 sqllite 转 mysql

从MSAccessDB迁移到SQLLite