mongodb数据库转存sqlite

最近公司项目中有需要,所以就写了,不多说了,先上代码

程序代码中引用的有其他人写的数据库执行和表创建函数,这里事先说明下(其实我就是整理了一下)

using System;
using System.Data.SQLite;
using MongoDB.Driver;
using MongoDB.Bson;

namespace Test
{
    class MongoMethod
    {
        MongoClient mongoClient;
        MongoServer mongoServer;
        MongoDatabase mongoDatabase;
        /// 
        /// 取出的数据缓存
        /// 
        BsonDocument[] dataStrBuffers = new BsonDocument[3];
        //存储发送数据的字符串
        string sendData;

        private SQLiteConnection connection;
        SQLiteCommand m_DbCommand;
        SQLiteDataReader m_Reader;

        private string fileName = "(path,sqlite数据库文件所在路径)";

        static void Main(string[] args)
        {
            MongoMethod mongo = new MongoMethod();
            mongo.ConnectSqlite();
            mongo.GetData();
            Console.ReadKey(); 
        }

        private void ConnectSqlite()
        {
            connection = new SQLiteConnection(@"Data Source=" + fileName);
            connection.Open();
        }

        /// 
        /// 执行数据库语句(返回数据集)
        /// 
        /// 数据库语句
        /// 执行结果返回的数据集
        public SQLiteDataReader ExecuteQuery(string sqlQuery)
        {
            if (connection == null)
            {
                Console.WriteLine("SQLite connection is null, Please OpenDB first");

                return null;
            }

            m_DbCommand = connection.CreateCommand();
            m_DbCommand.CommandText = sqlQuery;
            m_Reader = m_DbCommand.ExecuteReader();

            return m_Reader;
        }

        /// 
        /// 执行数据库语句(不返回结果集)
        /// 
        /// 数据库语句
        public void ExecuteNonQuery(string sqlQuery)
        {
            if (connection == null)
            {
                Console.WriteLine("SQLite connection is null, Please OpenDB first");

                return;
            }

            m_DbCommand = connection.CreateCommand();
            m_DbCommand.CommandText = sqlQuery;
            m_DbCommand.ExecuteNonQuery();
        }

        /// 
        /// 创建数据库新表
        /// 
        /// 数据库表名
        /// 目标列名集合
        /// 目标类型名集合
        /// 执行结果数据集读取器
        public SQLiteDataReader CreateTable(string tableName, string[] cols, string[] colTypes)
        {
            if (cols.Length != colTypes.Length)
            {
                Console.WriteLine("columns.Length != colType.Length");
            }

            string query = "CREATE TABLE " + tableName + " (" + cols[0] + " " + colTypes[0];
            for (int i = 1; i < cols.Length; ++i)
            {
                query += ", " + cols[i] + " " + colTypes[i];
            }
            query += ")";

            return ExecuteQuery(query);
        }
          
        /// 
        /// 插入数据
        /// 
        /// 数据库表名
        /// 目标列名集合
        /// 数据
        public void InsertIntoSpecific(string tableName, string colName, string value)
        {
            try
            {
                string query = string.Format("INSERT INTO {0}({1}) VALUES ('{2}')", tableName, colName, value);

                ExecuteNonQuery(query);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void GetData()
        {   
            //连接mongodb数据库
            mongoClient = new MongoClient("mongodb://localhost:27017");
            mongoServer = mongoClient.GetServer();
            mongoDatabase = mongoServer.GetDatabase("ZWAR");//根据mongo数据库名获取数据库
            //获取表单
            var collection = mongoDatabase.GetCollection("ZR2");
            //查找表中所有数据
            MongoCursor mongoCursor = collection.FindAll();
            mongoCursor.SetFlags(QueryFlags.NoCursorTimeout);
            foreach (BsonDocument book in mongoCursor)
            {
                    #region 数据格式整理
            
                    ................
                    #endregion
                    //更新要发送的数据
                    sendData = ;
                    
                    //打印数据到控制台上
                    Console.WriteLine(sendData);
                    //插入数据
                    InsertIntoSpecific("allSignalData10", "content",sendData);
                }
            }
        }
    }
}

 

你可能感兴趣的:(实用代码整理)