C# else if((读取器[j].GetType()==typeof(Int32))) { 添加(新的bsoneElement(reader.GetName(j),BsonValue.Create(reader.GetInt32(j)); } else if
else if((读取器[j].GetType()==typeof(Int32))) { 添加(新的bsoneElement(reader.GetName(j),BsonValue.Create(reader.GetInt32(j)); } else if(reader[j].GetType()==typeof(Int16)) { 添加(新的bsoneElement(reader.GetName(j),BsonValue.Create(reader.GetInt16(j)); } else if(reader[j].GetType()==typeof(Int64)) { 添加(新的bsoneElement(reader.GetName(j),BsonValue.Create(reader.GetInt64(j)); } else if(reader[j].GetType()==typeof(float)) { 添加(新的bsonement(reader.GetName(j),BsonValue.Create(reader.GetFloat(j))); } else if(reader[j].GetType()==typeof(Double)) { 添加(新的bsonement(reader.GetName(j),BsonValue.Create(reader.GetDouble(j))); } else if(reader[j].GetType()==typeof(DateTime)) { 添加(新的bsonement(reader.GetName(j),BsonValue.Create(reader.GetDateTime(j))); } else if(读取器[j].GetType()==typeof(Guid)) 添加(新的bsonement(reader.GetName(j),BsonValue.Create(reader.GetGuid(j))); else if(读取器[j].GetType()==typeof(布尔)) { 添加(新的bsonement(reader.GetName(j),BsonValue.Create(reader.GetBoolean(j))); } else if(reader[j].GetType()==typeof(DBNull)) { Add(新的BsonElement(reader.GetName(j),BsonNull.Value)); } else if(读取器[j].GetType()==typeof(字节)) { 添加(新的bsoneElement(reader.GetName(j),BsonValue.Create(reader.GetByte(j))); } else if(reader[j].GetType()==typeof(Byte[])) { 添加(新的bsonement(reader.GetName(j),BsonValue.Create(reader[j]作为字节[])); } 其他的 抛出新异常(); } bsonlist.Add(bson); } 如果(i>0) { 使用(server.RequestStart(db)) { //蒙戈集合 coll=db.GetCollection(表); coll.InsertBatch(bsonlist); bsonlist.RemoveRange(0,bsonlist.Count); } i=0; } } } } }C# else if((读取器[j].GetType()==typeof(Int32))) { 添加(新的bsoneElement(reader.GetName(j),BsonValue.Create(reader.GetInt32(j)); } else if,c#,sql,mongodb,C#,Sql,Mongodb,else if((读取器[j].GetType()==typeof(Int32))) { 添加(新的bsoneElement(reader.GetName(j),BsonValue.Create(reader.GetInt32(j)); } else if(reader[j].GetType()==typeof(Int16)) { 添加(新的bsoneElement(reader.GetName(j),BsonValue.Create(reader.GetInt16(j)); } else if
MongoDB的理念是尽可能多地将这类事情推到驱动程序/应用程序级别。。。所以我同意下面马特的观点。。。你最好的选择是使用C#不仅因为它会为你节省很多麻烦(使用它很好的BSON lib),还因为你可以处理许多数据转换问题,比转储或导出等更容易。除了日期格式,Mongo在转换过程中还有其他类型不喜欢的吗?日期是我们真正的杀手,因为mongo将其视为自纪元以来的64位数字,而我们使用的所有其他数据都使用32位日期。我们在tsv模式下导入时遇到问题,因为根据10gen,引用的字符串在tsv文件中不能有换行符。我们也遇到了其他问题,但这些问题对我们的工具来说更为具体。要使用MongoServer、MongoDatabase等类型,我需要添加哪些参考资料?我有MongoDB.BSON、MongoDB.Driver和MongoDB.Driver.Core,但这些行代码在VIsual Studio中是红色的。我也希望运行这些代码,但无法使用当前的MongoDB驱动程序包进行编译。找不到MongoServer、MongoDatabase类?谢谢,洛杉矶佬
var r=XmlReader.Create("file://D:/1.xml");
XmlDocument xdoc=new XmlDocument();
xdoc.Load(r);
string result="";
//o["Root"]["Airport"];
foreach(XmlNode n in xdoc.ChildNodes[0]){
var rr= JsonConvert.SerializeXmlNode(n);
JObject o=JObject.Parse(rr);
var co=o.Children().Children().First();
foreach (JToken c in co.Children().Where(cc=>cc.Type==JTokenType.Property).ToList()){
var prop=c as JProperty;
double d;
if (double.TryParse(co[prop.Name].Value<string>(),out d))
{
co[prop.Name] = d;
}
//c.Value<string>().Dump();
//c.Value<string>().Dump();
//co[c.Name]
}
//co["APT_Latitude"].Value<decimal>().Dump();
result=result + co.ToString(Newtonsoft.Json.Formatting.None)+"\r\n";
}
File.WriteAllText("D:/1.json",result);
//result.Dump();
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;
}
}
}
}
}