Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

c# - Convert SQL table to mongoDB document

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 row in the table and saves that in Mongo.. However, I am looking for a better way to convert a lot of data..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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<string> tablelist = new List<string>();
    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<MongoDB.Bson.BsonDocument> coll = db.GetCollection<BsonDocument>("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<BsonDocument> collection = db.GetCollection<BsonDocument>(table);
                    collection.Drop();
                }
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                List<BsonDocument> bsonlist = new List<BsonDocument>(1000);
                while (reader.Read())
                {
                    if (i == 1000)
                    {
                        using (server.RequestStart(db))
                        {
                            //MongoCollection<MongoDB.Bson.BsonDocument> 
                            coll = db.GetCollection<BsonDocument>(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<MongoDB.Bson.BsonDocument> 
                        coll = db.GetCollection<BsonDocument>(table);
                        coll.InsertBatch(bsonlist);
                        bsonlist.RemoveRange(0, bsonlist.Count);
                    }
                    i = 0;
                }
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...