datatable写入sqlite

    public class DataTableToSQLte
    {
        private string tableName;

        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }
        private string insertHead;

        public string InsertHead
        {
            get { return insertHead; }
        }

        private string[] separators;

        public string[] Separators
        {
            get { return separators; }
            set { separators = value; }
        }

        private string insertCmdText;

        private int colCount;
        private string[] fields;

        public DataTableToSQLte(DataTable dt)
        {
            List<string> myFields = new List<string>();
            List<string> mySeparators = new List<string>();
            List<string> valueVars = new List<string>();// insert command text
            colCount = dt.Columns.Count;

            for (int i = 0; i < colCount; i++)
            {
                string colName = dt.Columns[i].ColumnName;
                myFields.Add(colName);
                mySeparators.Add(GetSeperator(dt.Columns[i].DataType.ToString()));
                valueVars.Add("@" + colName);
            }
            insertHead = string.Format("insert into {0} ({1})"
                , dt.TableName
                , string.Join(",", myFields.ToArray()));
            separators = mySeparators.ToArray();

            insertCmdText = string.Format("{0} values ({1})", insertHead
                , string.Join(",", valueVars.ToArray()));

            fields = myFields.ToArray();

        }

        private string GetSeperator(string typeName)
        {
            string result = string.Empty;
            switch (typeName)
            {
                case "System.String":
                    result = "'";
                    break;

                default:
                    result = typeName;
                    break;
            }

            return result;
        }


        public string GenInsertSql(DataRow dr)
        {
            List<string> strs = new List<string>();
            for (int i = 0; i < colCount; i++)
            {
                if (DBNull.Value == dr[i])  //null or DBNull
                    strs.Add("null");
                else
                    strs.Add(string.Format("{0}{1}{0}", separators[i], dr[i].ToString()));
            }
            return string.Format("{0} values ({1})", insertHead, string.Join(",", strs.ToArray()));
        }

        public void ImportToSqliteBatch(DataTable dt, string dbFullName)
        {
            string strConn = string.Format("data source={0}", dbFullName);
            using (SQLiteConnection conn = new SQLiteConnection(strConn))
            {
                using (SQLiteCommand insertCmd = conn.CreateCommand())
                {
                    insertCmd.CommandText = insertCmdText;
                    conn.Open();
                    SQLiteTransaction tranction = conn.BeginTransaction();
                    foreach (DataRow dr in dt.Rows)
                    {
                        for (int i = 0; i < colCount; i++)
                        {
                            object o = null;
                            string paraName = "@" + fields[i];
                            if (DBNull.Value != dr[fields[i]])
                                o = dr[fields[i]];
                            insertCmd.Parameters.AddWithValue(paraName, o);
                        }
                        insertCmd.ExecuteNonQuery();
                    }
                    tranction.Commit();
                }
            }
        }


        private void Example()
        {
            string dbName = AppDomain.CurrentDomain.BaseDirectory + "test.db";
            DataTable dt = MyCommon.ConvertXmlToDataTable(MyCommon.ReadXmlStringFromFile("Dt1.xml"));
            DataTableToSQLte myTabInfo = new DataTableToSQLte(dt);
            myTabInfo.ImportToSqliteBatch(dt, dbName);
            //MessageBox.Show("Ok!");


        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值