C#连接示例
<p>本文主要介绍C#连接示例。</p>
<p> <img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20202805103006-1147fb21938d.png" style="height:26px; margin:0px; width:65px" />:官方资料可参考<a href="https://docs.mongodb.com/drivers/csharp" target="_blank">MongoDB C# Driver</a>。</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>
</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"> <strong>class</strong> 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 <strong>string</strong> host1 = "demotest.mongodb.db.cloud.papub";</span></p>
<p><span style="font-family:Consolas"> const <strong>int</strong> port1 = 15923;</span></p>
<p><span style="font-family:Consolas"> const <strong>string</strong> replicaSetName = "demotest";</span></p>
<p><span style="font-family:Consolas"> const <strong>string</strong> admin = "test";</span></p>
<p><span style="font-family:Consolas"> const <strong>string</strong> userName = "demouser";</span></p>
<p><span style="font-family:Consolas"> const <strong>string</strong> passwd = "123456";</span></p>
<p> </p>
<p><span style="font-family:Consolas"> <strong>try</strong></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 = <strong>new</strong> MongoClientSettings();</span></p>
<p><span style="font-family:Consolas"> List<MongoServerAddress> servers = <strong>new</strong> 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 = <strong>new</strong> 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 = <strong>new</strong> 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 = <strong>new</strong> 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"> <strong>class</strong> User</span></p>
<p><span style="font-family:Consolas"> {</span></p>
<p><span style="font-family:Consolas"> public <strong>string</strong> id { set; get; }</span></p>
<p><span style="font-family:Consolas"> public <strong>string</strong> name { set; get; }</span></p>
<p><span style="font-family:Consolas"> public <strong>string</strong> 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>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!