Convert SQL table to mongoDB document

后端 未结 5 1778
眼角桃花
眼角桃花 2020-12-15 01:21

What\'s the best way to convert a SQL database, let\'s say 1 table, to a mongoDB document?

I guess I could use a C# driver and implement a loop, which selects each r

5条回答
  •  执念已碎
    2020-12-15 02:04

    This is the import script that I am using for Importing data from SQL server to Mongodb located on my box. This code will just create a similar table(existing in SQL DB) in MongoDB. You can supply the table list to import as comma seperated and all of them would be imported without problems.

    static void Main(string[] args)
    {
        List tablelist = new List();
        if (!args[0].Contains(','))
            tablelist.Add(args[0]);
        else
            tablelist.AddRange(args[0].Split(','));
        string sqlconnectionstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        var connectionString = "mongodb://localhost/?safe=true;w=1;wtimeout=30s";
        var safemode = SafeMode.True;
        MongoServer server = MongoServer.Create(connectionString);
        MongoDatabase db = server.GetDatabase("testdb");
        MongoCollection coll = db.GetCollection("test");
        //coll.Find().Count();
        int i = 0;
        foreach (string table in tablelist)
        {
    
            using (SqlConnection conn = new SqlConnection(sqlconnectionstring))
            {
                string query = "select * from " + table;
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    /// Delete the MongoDb Collection first to proceed with data insertion
    
                    if (db.CollectionExists(table))
                    {
                        MongoCollection collection = db.GetCollection(table);
                        collection.Drop();
                    }
                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    List bsonlist = new List(1000);
                    while (reader.Read())
                    {
                        if (i == 1000)
                        {
                            using (server.RequestStart(db))
                            {
                                //MongoCollection 
                                coll = db.GetCollection(table);
                                coll.InsertBatch(bsonlist);
                                bsonlist.RemoveRange(0, bsonlist.Count);
                            }
                            i = 0;
                        }
                        ++i;
                        BsonDocument bson = new BsonDocument();
                        for (int j = 0; j < reader.FieldCount; j++)
                        {
                            if (reader[j].GetType() == typeof(String))
                                bson.Add(new BsonElement(reader.GetName(j), reader[j].ToString()));
                            else if ((reader[j].GetType() == typeof(Int32)))
                            {
                                bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetInt32(j))));
                            }
                            else if (reader[j].GetType() == typeof(Int16))
                            {
                                bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetInt16(j))));
                            }
                            else if (reader[j].GetType() == typeof(Int64))
                            {
                                bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetInt64(j))));
                            }
                            else if (reader[j].GetType() == typeof(float))
                            {
                                bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetFloat(j))));
                            }
                            else if (reader[j].GetType() == typeof(Double))
                            {
                                bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetDouble(j))));
                            }
                            else if (reader[j].GetType() == typeof(DateTime))
                            {
                                bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetDateTime(j))));
                            }
                            else if (reader[j].GetType() == typeof(Guid))
                                bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetGuid(j))));
                            else if (reader[j].GetType() == typeof(Boolean))
                            {
                                bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetBoolean(j))));
                            }
                            else if (reader[j].GetType() == typeof(DBNull))
                            {
                                bson.Add(new BsonElement(reader.GetName(j), BsonNull.Value));
                            }
                            else if (reader[j].GetType() == typeof(Byte))
                            {
                                bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetByte(j))));
                            }
                            else if (reader[j].GetType() == typeof(Byte[]))
                            {
                                bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader[j] as Byte[])));
                            }
                            else
                                throw new Exception();
                        }
                        bsonlist.Add(bson);
                    }
                    if (i > 0)
                    {
                        using (server.RequestStart(db))
                        {
                            //MongoCollection 
                            coll = db.GetCollection(table);
                            coll.InsertBatch(bsonlist);
                            bsonlist.RemoveRange(0, bsonlist.Count);
                        }
                        i = 0;
                    }
                }
            }
        }
    }
    

提交回复
热议问题