private void putMongoDBData(xFerDb db, DataTable dt, xFerTransferType t, string mongoCollection) { if (dt != null) { if (dt.Rows.Count > 0) { try { Console.WriteLine("Insert mongodb begin"); MongoServer server = MongoServer.Create(db.ConnectionString); MongoDatabase database = server.GetDatabase(db.DbName); // "test" is the name of the database MongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>(mongoCollection); if (t == xFerTransferType.REPLACE) { collection.RemoveAll(); } List<BsonDocument> docs = new List<BsonDocument>(); foreach (DataRow dr in dt.Rows) { BsonDocument d = new BsonDocument(); foreach (DataColumn dc in dt.Columns) { Type colType = dr[dc.ColumnName].GetType(); string val = string.Empty; if (colType.FullName != "System.String") { if (colType.FullName == "System.DateTime") { d.Add(dc.ColumnName.ToString(), Convert.ToDateTime(dr[dc.ColumnName].ToString())); } else if (colType.FullName == "System.Int32") { d.Add(dc.ColumnName.ToString(), Convert.ToInt32(dr[dc.ColumnName].ToString())); } else if (colType.FullName == "System.Int64") { d.Add(dc.ColumnName.ToString(), Convert.ToInt64(dr[dc.ColumnName].ToString())); } else if (colType.FullName == "System.Boolean") { d.Add(dc.ColumnName.ToString(), Convert.ToBoolean(dr[dc.ColumnName].ToString())); } else if (colType.FullName == "System.Decimal") { d.Add(dc.ColumnName.ToString(), Convert.ToDouble(dr[dc.ColumnName].ToString())); } } else { d.Add(dc.ColumnName.ToString(), dr[dc.ColumnName].ToString()); } } docs.Add(d); } collection.InsertBatch(docs); Console.WriteLine("Insert mongodb finish: rows added to " + db.DbName + "." + mongoCollection + "(" + dt.Rows.Count + ")"); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message.ToString()); } } else { Console.WriteLine("Source failed to return data."); } } else { Console.WriteLine("Source failed to return data."); } }
public void putSqlDbData(xFerDb db, DataTable dt, xFerTransferType t, xFerQuery q) { CommandType ct = CommandType.StoredProcedure; if (dt != null) { if (dt.Rows.Count > 0) { if (q.QueryType == xFerQueryType.Text) { ct = CommandType.Text; } if (q.QueryText.ToString() != string.Empty && db.ConnectionString != string.Empty) { SqlConnection oSqlConn = new SqlConnection(db.ConnectionString); SqlCommand com = new SqlCommand(); try { com.Connection = oSqlConn; com.CommandTimeout = db.CommandTimeout; com.Connection = oSqlConn; com.CommandType = ct; com.CommandText = q.QueryText.ToString(); com.Connection.Open(); foreach (DataRow dr in dt.Rows) { if (com.Parameters.Count > 0) { com.Parameters.Clear(); } if (q.Paramaters != null) { if (q.Paramaters.Count == 0) { foreach (DataColumn c in dt.Columns) { q.Paramaters.Add(new xFerParamater(c.ColumnName.Trim(), "")); } } foreach (xFerParamater p in q.Paramaters) { try { com.Parameters.AddWithValue("@" + p.Name, dr[p.Name].ToString()); } catch { _errors.Add("Insert missing column " + p.Name); } } } com.ExecuteNonQuery(); } com.Connection.Close(); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message.ToString()); } finally { if (com.Connection.State == ConnectionState.Open) { com.Connection.Close(); } } } } else { Console.WriteLine("Source failed to return data."); } } else { Console.WriteLine("Source failed to return data."); } }
private void putData(xFerDb db, DataTable dt, xFerTransferType t, xFerQuery xOut, string mongoCollection) { switch (db.DbType) { case xFerDbType.mongoDb: putMongoDBData(db, dt, t, mongoCollection); break; case xFerDbType.MSSQL: putSqlDbData(db, dt, t, xOut); break; case xFerDbType.MySql: putMySqlDbData(db, dt, t, xOut); break; } }