C#连接示例
<p>本文主要介绍C#连接示例。</p>
<p><img src="https://obs-cn-shanghai.ocftcloud.com/pacloud/20202805144352-153c12ac958f.png" style="height:26px; margin:0px; width:65px" />:官方资料可参考MongoDB C# Driver。</p>
<p><span style="font-size:18px"><strong>操作步骤</strong></span></p>
<p>1. 获取 DDS 实例的连接信息。</p>
<p>2. 执行以下命令,安装驱动包。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">mongocsharpdriver.dll</span></p>
<p> </p>
</td>
</tr>
</tbody>
</table>
<p>3. C#安装代码:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">using MongoDB.Driver;</span></p>
<p><span style="font-family:Consolas">using System;</span></p>
<p><span style="font-family:Consolas">using System.Collections.Generic;</span></p>
<p> </p>
<p><span style="font-family:Consolas">namespace Aliyun</span></p>
<p><span style="font-family:Consolas">{</span></p>
<p><span style="font-family:Consolas"> class Program</span></p>
<p><span style="font-family:Consolas"> {</span></p>
<p><span style="font-family:Consolas"> static void Main(string[] args)</span></p>
<p><span style="font-family:Consolas"> {</span></p>
<p><span style="font-family:Consolas"> //Mongo 实例信息</span></p>
<p><span style="font-family:Consolas"> const string host1 = "demotest.mongodb.db.cloud.papub";</span></p>
<p><span style="font-family:Consolas"> const int port1 = 15923;</span></p>
<p><span style="font-family:Consolas"> const string replicaSetName = "demotest";</span></p>
<p><span style="font-family:Consolas"> const string admin = "test";</span></p>
<p><span style="font-family:Consolas"> const string userName = "demouser";</span></p>
<p><span style="font-family:Consolas"> const string passwd = "123456";</span></p>
<p> </p>
<p><span style="font-family:Consolas"> try</span></p>
<p><span style="font-family:Consolas"> {</span></p>
<p><span style="font-family:Consolas"> Console.WriteLine("开始连接.......");</span></p>
<p><span style="font-family:Consolas"> MongoClientSettings settings = new MongoClientSettings();</span></p>
<p><span style="font-family:Consolas"> List<MongoServerAddress> servers = new List<MongoServerAddress>();</span></p>
<p><span style="font-family:Consolas"> servers.Add(new MongoServerAddress(host1, port1));</span></p>
<p><span style="font-family:Consolas"> settings.Servers = servers;</span></p>
<p><span style="font-family:Consolas"> //设置副本集名称</span></p>
<p><span style="font-family:Consolas"> settings.ReplicaSetName = replicaSetName;</span></p>
<p><span style="font-family:Consolas"> //设置超时时间为3秒</span></p>
<p><span style="font-family:Consolas"> settings.ConnectTimeout = new TimeSpan(0, 0, 0, 3, 0);</span></p>
<p><span style="font-family:Consolas"> MongoCredential credentials = MongoCredential.CreateCredential(admin, userName, passwd);</span></p>
<p><span style="font-family:Consolas"> settings.Credential = credentials;</span></p>
<p><span style="font-family:Consolas"> MongoClient client = new MongoClient(settings);</span></p>
<p><span style="font-family:Consolas"> var server = client.GetServer();</span></p>
<p><span style="font-family:Consolas"> MongoDatabase database = server.GetDatabase("test");</span></p>
<p><span style="font-family:Consolas"> var collection = database.GetCollection<User>("test_collection");</span></p>
<p><span style="font-family:Consolas"> User user = new User();</span></p>
<p><span style="font-family:Consolas"> user.id = "1";</span></p>
<p><span style="font-family:Consolas"> user.name = "mongo_test";</span></p>
<p><span style="font-family:Consolas"> user.sex = "女";</span></p>
<p><span style="font-family:Consolas"> //插入数据user</span></p>
<p><span style="font-family:Consolas"> collection.Insert(user);</span></p>
<p><span style="font-family:Consolas"> //获取一条数据</span></p>
<p><span style="font-family:Consolas"> User result = collection.FindOne();</span></p>
<p><span style="font-family:Consolas"> Console.WriteLine("id:" + result.id + " name:" + result.name + " sex:"+result.sex);</span></p>
<p><span style="font-family:Consolas"> Console.WriteLine("连接成功.........");</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> catch (Exception e)</span></p>
<p><span style="font-family:Consolas"> {</span></p>
<p><span style="font-family:Consolas"> Console.WriteLine("连接异常:"+e.Message);</span></p>
<p> </p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> class User</span></p>
<p><span style="font-family:Consolas"> {</span></p>
<p><span style="font-family:Consolas"> public string id { set; get; }</span></p>
<p><span style="font-family:Consolas"> public string name { set; get; }</span></p>
<p><span style="font-family:Consolas"> public string sex { set; get; }</span></p>
<p> </p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas">}</span></p>
</td>
</tr>
</tbody>
</table>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!