using Systemusing System.Collections.Genericusing System.Textusing System.Linqusing MongoDB.Bsonusing MongoDB.Driverusing MongoDB.Bson.Serialization.Attributesusing System.Linq.Expressionsusing MongoDB.Driver.Buildersusing MongoDB.Driver.Linqusing System.Configurationnamespace mongoDbOper
{public class DBHelper
{/// <summary>
/// mongodb数茄芦据库连接字符串/// </summary>
private static string dbConnectStr = "mongodb://127.0.0.1:27017" /// <summary>
/// 指定的数据库/// </summary>
private static string dbName = "OTT_DB" /// <summary>
/// 展会信息表/// </summary>
private static string tbNameM = "Meeting" /// <summary>
/// 行业岁正资讯表/// </summary>
private static string tbNameC = "Information" /// <summary>
/// 订阅用户表/// </颤雀带summary>
private static string tbNameUser = "User" /// <summary>
/// 已发送展会信息表/// </summary>
private static string tbNameMCK = "MeetingCK" /// <summary>
/// 已发送行业资讯表/// </summary>
private static string tbNameICK = "InfoCK" private static MongoDatabase db = null /// <summary>
/// 用户审核通过/// </summary>
private static string userPass = "通过" /// <summary>
/// 用户未审核通过,待审核/// </summary>
private static string userNoPass = "审核" /// <summary>
/// 初始化mongodb数据库/// </summary>
public static void InitDB()
{var connStr = string.IsNullOrEmpty(ConfigurationManager.AppSettings["mongodbServer"]) ? dbConnectStr : ConfigurationManager.AppSettings["mongodbServer"].ToString()
userPass = string.IsNullOrEmpty(ConfigurationManager.AppSettings["pass"]) ? userPass : ConfigurationManager.AppSettings["pass"].ToString()
userNoPass = string.IsNullOrEmpty(ConfigurationManager.AppSettings["pass"]) ? userNoPass : ConfigurationManager.AppSettings["nonpass"].ToString() //创建数据连接
//var server = MongoServer.Create(connStr)
var server = new MongoClient(connStr).GetServer() //获取指定数据库
db = server.GetDatabase(dbName)
}
//保存新用户的信息
public static bool InsertUser(User user = null, String tbName = "User")
{if (db == null)
{
InitDB()
}
User user1 = new User()
user1.name = "test"
user1.email = "test@ctrchina.cn"
user1.tel = "13388989998"
user1.company = "CTR"
user1.position = "总监"
user1.isPass = "通过"
MongoCollection<User>col = db.GetCollection<User>(tbName)
WriteConcernResult result = col.Insert(user1)
Console.WriteLine(result.Ok)
Console.Read() return result.Ok
}
}
}
在 Java 程序中如果要使用 MongoDB,你需要确团哗保已经塌碰行安装了 Java 环境及 MongoDB JDBC 驱动。
连接数据库,你需要指定数据库吵丛名称,如果指定的数据库不存在,mongo会自动创建数据库。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)