网上代码太直接了,都不验证用户身份直接写库
[C#] 纯文本查看 复制代码 //数据库连接字符串
string conn = "mongodb://127.0.0.1:27017";
//数据库名称
string database = "RsdfDb";
string collection = "Act_User";
MongoServer mongodb = MongoServer.Create(conn);//连接数据库
MongoDatabase mongoDataBase = mongodb.GetDatabase(database);//选择数据库名
MongoCollection mongoCollection = mongoDataBase.GetCollection(collection);//选择集合,相当于表
mongodb.Connect();
//普通插入
var o = new { UserID = 0, UserName = "admin", Password = "1" };
mongoCollection.Insert(o);
//对象插入
User user = new User { UserID = 1, UserName = "chenqp", Password = "1" };
mongoCollection.Insert(user);
//BsonDocument 插入
BsonDocument bd = new BsonDocument();
bd.Add("UserID", 2);
bd.Add("UserName", "yangh");
bd.Add("Password", "1");
mongoCollection.Insert(bd);
|