本文整理汇总了C#中MongoDB.Driver.MongoConnectionStringBuilder类的典型用法代码示例。如果您正苦于以下问题:C# MongoConnectionStringBuilder类的具体用法?C# MongoConnectionStringBuilder怎么用?C# MongoConnectionStringBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MongoConnectionStringBuilder类属于MongoDB.Driver命名空间,在下文中一共展示了MongoConnectionStringBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
MongoConnectionStringBuilder builder = new MongoConnectionStringBuilder();
builder.Server = new MongoServerAddress("localhost", 27017);
builder.DatabaseName = "douban";
MongoServer mongo = MongoServer.Create(builder);
mongo.Connect();
var db = mongo.GetDatabase("douban");
SqlConnectionStringBuilder sqlconnbuilder = new SqlConnectionStringBuilder();
sqlconnbuilder.DataSource = "localhost\\s2012";
sqlconnbuilder.InitialCatalog = "douban";
sqlconnbuilder.IntegratedSecurity = true;
SqlConnection sql = new SqlConnection(sqlconnbuilder.ToString());
sql.Open();
var cmd = sql.CreateCommand();
InsertGroup(mongo, db, cmd);
//InsertGroupPost(mongo, db, cmd);
}
示例2: TestConnectTimeout
public void TestConnectTimeout()
{
string connectionString = "server=localhost;connectTimeout=123";
var builder = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(TimeSpan.FromSeconds(123), builder.ConnectTimeout);
Assert.AreEqual(connectionString + "s", builder.ToString()); // "s" units added
connectionString = "server=localhost;connectTimeout=123ms";
builder = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(TimeSpan.FromMilliseconds(123), builder.ConnectTimeout);
Assert.AreEqual(connectionString, builder.ToString());
connectionString = "server=localhost;connectTimeout=123s";
builder = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(TimeSpan.FromSeconds(123), builder.ConnectTimeout);
Assert.AreEqual(connectionString, builder.ToString());
connectionString = "server=localhost;connectTimeout=123m";
builder = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(TimeSpan.FromMinutes(123), builder.ConnectTimeout);
Assert.AreEqual(connectionString, builder.ToString());
connectionString = "server=localhost;connectTimeout=123h";
builder = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(TimeSpan.FromHours(123), builder.ConnectTimeout);
Assert.AreEqual(connectionString, builder.ToString());
}
示例3: MongoBind
public MongoBind(string host, int port, string database, string collection)
{
var builder = new MongoConnectionStringBuilder {Server = new MongoServerAddress(host, port)};
Client = new MongoClient(builder.ConnectionString);
Database = Client.GetServer().GetDatabase(database);
Collection = Database.GetCollection(collection);
}
示例4: TestDefaults
public void TestDefaults() {
string connectionString = "server=localhost";
var builder = new MongoConnectionStringBuilder(connectionString);
Assert.IsNull(builder.Username);
Assert.IsNull(builder.Password);
Assert.AreEqual(1, builder.Servers.Count());
Assert.AreEqual("localhost", builder.Servers.Single().Host);
Assert.AreEqual(27017, builder.Servers.Single().Port);
Assert.IsNull(builder.DatabaseName);
Assert.AreEqual(ConnectionMode.Direct, builder.ConnectionMode);
Assert.AreEqual(MongoDefaults.ConnectTimeout, builder.ConnectTimeout);
Assert.AreEqual(MongoDefaults.GuidRepresentation, builder.GuidRepresentation);
Assert.AreEqual(false, builder.IPv6);
Assert.AreEqual(MongoDefaults.MaxConnectionIdleTime, builder.MaxConnectionIdleTime);
Assert.AreEqual(MongoDefaults.MaxConnectionLifeTime, builder.MaxConnectionLifeTime);
Assert.AreEqual(MongoDefaults.MaxConnectionPoolSize, builder.MaxConnectionPoolSize);
Assert.AreEqual(null, builder.ReplicaSetName);
Assert.IsNull(builder.SafeMode);
Assert.AreEqual(false, builder.SlaveOk);
Assert.AreEqual(MongoDefaults.SocketTimeout, builder.SocketTimeout);
Assert.AreEqual(MongoDefaults.WaitQueueMultiple, builder.WaitQueueMultiple);
Assert.AreEqual(MongoDefaults.WaitQueueSize, builder.WaitQueueSize);
Assert.AreEqual(MongoDefaults.WaitQueueTimeout, builder.WaitQueueTimeout);
Assert.AreEqual(connectionString, builder.ToString());
}
示例5: btConnect_Click
private void btConnect_Click(object sender, EventArgs e)
{
if(!ServerConnection.Connected)
{
try
{
var connectionString = new MongoConnectionStringBuilder();
connectionString.Server = new MongoServerAddress(tbServerAddress.Text);
connectionString.Username = tbUser.Text;
connectionString.Password = tbPassword.Text;
connectionString.DatabaseName = "test";
//new ServerConnection(tbServerAddress.Text);
new ServerConnection(connectionString);
//FillComboboxWithDatabaseNames(ServerConnection.Server);
SetConnectedState();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
tbServerAddress.Focus();
}
}
else
{
//ServerConnection.DatabaseName = cbDatabases.Text;
ServerConnection.DatabaseName = "test";
CloseLoginFormOpenMain();
}
}
示例6: TestDefaults
public void TestDefaults()
{
var builder = new MongoConnectionStringBuilder();
Assert.AreEqual(null, builder.Username);
Assert.AreEqual(null, builder.Password);
Assert.AreEqual(null, builder.Server);
Assert.AreEqual(null, builder.Servers);
Assert.AreEqual(null, builder.DatabaseName);
Assert.AreEqual(ConnectionMode.Automatic, builder.ConnectionMode);
Assert.AreEqual(MongoDefaults.ConnectTimeout, builder.ConnectTimeout);
Assert.AreEqual(MongoDefaults.GuidRepresentation, builder.GuidRepresentation);
Assert.AreEqual(false, builder.IPv6);
Assert.AreEqual(MongoDefaults.MaxConnectionIdleTime, builder.MaxConnectionIdleTime);
Assert.AreEqual(MongoDefaults.MaxConnectionLifeTime, builder.MaxConnectionLifeTime);
Assert.AreEqual(MongoDefaults.MaxConnectionPoolSize, builder.MaxConnectionPoolSize);
Assert.AreEqual(MongoDefaults.MinConnectionPoolSize, builder.MinConnectionPoolSize);
Assert.AreEqual(null, builder.ReadPreference);
Assert.AreEqual(null, builder.ReplicaSetName);
Assert.AreEqual(null, builder.SafeMode);
Assert.AreEqual(MongoDefaults.SecondaryAcceptableLatency, builder.SecondaryAcceptableLatency);
#pragma warning disable 618
Assert.AreEqual(false, builder.SlaveOk);
#pragma warning restore
Assert.AreEqual(MongoDefaults.SocketTimeout, builder.SocketTimeout);
Assert.AreEqual(false, builder.UseSsl);
Assert.AreEqual(MongoDefaults.WaitQueueMultiple, builder.WaitQueueMultiple);
Assert.AreEqual(MongoDefaults.WaitQueueSize, builder.WaitQueueSize);
Assert.AreEqual(MongoDefaults.WaitQueueTimeout, builder.WaitQueueTimeout);
Assert.AreEqual(MongoDefaults.ComputedWaitQueueSize, builder.ComputedWaitQueueSize);
var connectionString = ""; // not actually a valid connection string because it's missing the host
Assert.AreEqual(connectionString, builder.ToString());
}
示例7: TestToStringOutput
public void TestToStringOutput()
{
var builder = new MongoConnectionStringBuilder { Password = "testpassword", Username = "testusername", ConnectionLifetime = TimeSpan.FromSeconds (50), MaximumPoolSize = 101, MinimumPoolSize = 202, ConnectionTimeout = TimeSpan.FromSeconds(60)};
builder.AddServer ("testserver1", 555);
builder.AddServer ("testserver2");
Assert.AreEqual ("Username=testusername;Password=testpassword;Server=testserver1:555,testserver2;MaximumPoolSize=101;MinimumPoolSize=202;ConnectionTimeout=60;ConnectionLifetime=50", builder.ToString ());
}
示例8: Connect
public static CountersDatabase Connect(string host, int? port, string dbName)
{
MongoConnectionStringBuilder builder = new MongoConnectionStringBuilder();
builder.SocketTimeout = new TimeSpan(0, 30, 0);
builder.Server = port.HasValue ? new MongoServerAddress(host, port.Value) : new MongoServerAddress(host);
MongoServer server = MongoServer.Create(builder);
server.Connect();
MongoDatabase db = server.GetDatabase(dbName);
return new CountersDatabase(server, db);
}
示例9: TestConnectionStringParsingServerWithoutPort
public void TestConnectionStringParsingServerWithoutPort()
{
var builder = new MongoConnectionStringBuilder ("Username=testuser;Password=testpassword;Server=testserver");
Assert.AreEqual ("testuser", builder.Username);
Assert.AreEqual ("testpassword", builder.Password);
var servers = new List<MongoServerEndPoint> (builder.Servers);
Assert.AreEqual (1, servers.Count);
Assert.AreEqual ("testserver", servers[0].Host);
Assert.AreEqual (MongoServerEndPoint.DefaultPort, servers[0].Port);
}
示例10: TestHostWithPort
public void TestHostWithPort() {
string connectionString = "server=mongo.xyz.com:12345";
var builder = new MongoConnectionStringBuilder(connectionString);
Assert.IsNull(builder.Username);
Assert.IsNull(builder.Password);
Assert.AreEqual(1, builder.Servers.Count());
Assert.AreEqual("mongo.xyz.com", builder.Servers.Single().Host);
Assert.AreEqual(12345, builder.Servers.Single().Port);
Assert.AreEqual(ConnectionMode.Direct, builder.ConnectionMode);
Assert.AreEqual(null, builder.ReplicaSetName);
Assert.AreEqual(connectionString, builder.ToString());
}
示例11: MyContext
public MyContext(string connString)
{
/*
var con = new MongoConnectionStringBuilder(connString);
var server = MongoServer.Create(con);
DB = server.GetDatabase(con.DatabaseName);
*/
var con = new MongoConnectionStringBuilder(connString);
var client = new MongoClient(connString);
var server = client.GetServer();
DB = server.GetDatabase(con.DatabaseName);
}
示例12: TestConnectionMode
public void TestConnectionMode()
{
string connectionString = "server=localhost;connect=direct";
var builder = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(ConnectionMode.Direct, builder.ConnectionMode);
Assert.AreEqual(connectionString, builder.ToString());
connectionString = "server=localhost;connect=replicaSet";
builder = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual(ConnectionMode.ReplicaSet, builder.ConnectionMode);
Assert.AreEqual(connectionString, builder.ToString());
}
示例13: MongoHelper
static MongoHelper()
{
if (Configuration.ConnectionString.StartsWith("mongodb://"))
{
IsConnectionUrl = true;
ConnectionUrl = new MongoUrlBuilder(Configuration.ConnectionString);
}
else
{
ConnectionString = new MongoConnectionStringBuilder(Configuration.ConnectionString);
}
}
示例14: ParseConnectionString
// private static methods
private static MongoClientSettings ParseConnectionString(string connectionString)
{
if (connectionString.StartsWith("mongodb://"))
{
var url = new MongoUrl(connectionString);
return MongoClientSettings.FromUrl(url);
}
else
{
var builder = new MongoConnectionStringBuilder(connectionString);
return MongoClientSettings.FromConnectionStringBuilder(builder);
}
}
示例15: TestOneIPv6AddressWithPort
public void TestOneIPv6AddressWithPort()
{
var connectionString = "mongodb://[::1:]:1234/?w=1";
var url = new MongoUrl(connectionString);
Assert.AreEqual("[::1:]", url.Server.Host);
Assert.AreEqual(1234, url.Server.Port);
Assert.AreEqual(true, url.SafeMode.Enabled);
connectionString = "server=[::1:]:1234;w=1";
var builder = new MongoConnectionStringBuilder(connectionString);
Assert.AreEqual("[::1:]", builder.Server.Host);
Assert.AreEqual(1234, builder.Server.Port);
Assert.AreEqual(true, builder.SafeMode.Enabled);
}