vsvb连接mysql,vs2019下⽤vb窗体程序连接mongodb4.2
说起来,查看Mongodb官⽅的接⼝⽂档是场噩梦,尽管mongodb官⽅花了⼤⼒⽓整顿了它的API,但是简单的接⼝罗列,0代码⽰范,让⼈⽆从开始。幸亏有很多天才,成功破译,我才得以沿着他们⾛的路,照猫画虎的⾛下去。整个项⽬结构如下:
类⽂件中vbtest.vb数据库实体类对应着mongodb⽂档vbtest,⽤于数据操作测试
ImportsMongoDB.BsonPublic ClassvbtestPublic _id AsObjectIdPublic content As String
End Class
(optional)vbmongo.vb是绑定好数据库实体类vbtest的版本
ImportsMongoDB.BsonImportsMongoDB.DriverPublic ClassvbmongoPublicclientPublic collection As
IMongoCollection(Ofvbtest)Public database AsIMongoDatabasePublic mlist As List(Ofvbtest)Public builder As FilterDefinitionBuilder(Of vbtest) = New FilterDefinitionBuilder(Ofvbtest)Public filter As FilterDefinition(Ofvbtest)Public Sub New(ByVal dbname As String, ByVal collectionname As String)
client= New MongoClient("mongodb://127.0.0.1:27017")
database=client.GetDatabase(dbname)
collection= database.GetCollection(Ofvbtest)(collectionname)End Sub
Public Async Function loadrecords() As Task(Of List(Ofvbtest))filter = builder.Ne(Of ObjectId)("_id", NewObjectId())//_id不等于空的记录,通过这种⽅法蹩脚的实现了查全部的功能Builder的⼤多数条件设置函数都⽤到了泛型Ne(Of TField)Dim mlist As List(Of vbtest) = Await collection.Find(filter).ToListAsync()//TField可以理解为 type of field 对应的是实体类中字段的数据类型例如OBjectId或者String等等ReturnmlistEnd Function
Public Async Function loadone(ByVal _id As String) As Task(Ofvbtest)filter = builder.Eq(Of ObjectId)("_id",
NewObjectId(_id))Dim mlist As vbtest = Await collection.Find(filter).FirstOrDefaultAsync()ReturnmlistEnd Function
Public Async Function updateone(ByVal _id As String, ByVal doc As vbtest) As Task(Ofvbtest)filter = builder.Eq(Of ObjectId)("_id", NewObjectId(_id))Dim up As UpdateDefinitionBuilder(Of vbtest) = New
UpdateDefinitionBuilder(Ofvbtest) ()Dim updef As UpdateDefinition(Ofvbtest)
updef= up.Set(Of String)("content", t)‘.Set(Of String)("content", doc)
collection.UpdateOneAsync(filter, updef)End Function
End Class
说起来mongodb的连接可以分⼏步,根据连接字符串打开链接,获取数据库,获取⽂档,进⽽在⽂档的基础上进⾏增删改查。对于vb 操作mongodb,完全是根据c#的操作范例,根据vb的语法特点临摹出来的。
⾸先需要对项⽬进⾏Nuget包的安装,我们要搜寻的是MongoDB.Driver
这⾥创建了⼀个vb 的 泛型类vbmongoT,只写了单个查,查列表和更新操作,函数中⼤量成对出现了Async和Await关键字,⽤于标识该功能使⽤了异步编程,在窗体程序中进⾏调⽤的时候,仍需要在成对的使⽤Async Await的关键字,因为异步函数返回的往往是Task类型(c#)或者 Task(Of TResult) (vb)需要在调⽤函数体中使⽤await关键字拿到最终结果
ImportsMongoDB.BsonImportsMongoDB.Driver
Public Class vbmongoT(OfT)PublicclientPublic collection As IMongoCollection(OfT)Public database AsIMongoDatabasePublic mlist As List(OfT)Public builder As FilterDefinitionBuilder(Of T) = New
FilterDefinitionBuilder(OfT)Public up As UpdateDefinitionBuilder(OfT)Public updef As UpdateDefinition(OfT)Public filter As FilterDefinition(OfT)Public Sub New(ByVal dbname As String, ByVal collectionname As String)
client= New MongoClient("mongodb://127.0.0.1:27017")
database=client.GetDatabase(dbname)
collection= database.GetCollection(OfT)(collectionname)End Sub
Public Async Function loadrecords() As Task(Of List(OfT))filter = builder.Ne(Of ObjectId)("_id", NewObjectId())Dim mlist As List(Of T) = Await collection.Find(filter).ToListAsync()ReturnmlistEnd Function
Public Async Function loadone(ByVal _id As String) As Task(OfT)filter = builder.Eq(Of ObjectId)("_id", NewObjectId(_id))//_id 满⾜查条件的记录,Builder的⼤多数条件设置函数都⽤到了泛型:例如Ne(Of TField)
Dim mlist As T = Await collection.Find(filter).FirstOrDefaultAsync()ReturnmlistEnd Function
Public Async Function updateone(ByVal filter As FilterDefinition(Of T), ByVal updef As UpdateDefinition(Of T)) As
Task(OfT)‘updef = up.Set(Of String)("content", t) ‘.Set(Of String)("content", doc)
Await collection.UpdateOneAsync(filter, updef)End Function
End Class
在mongodb这⼀版本的官⽅API⾥,IMongoCollection对象是查,添加,更新,删除等动作的发起者,这⾥⾯查,更新,删除都要⽤到查询条件,
官⽅谓之filter(过滤器)C#: FilterDefinition VB: FilterDefinition (Of T)
⽽过滤器是过滤器模具的建模结果
过滤器模具:C#: FilterDefinitionBuilder VB: FilterDefinitionBuilder (Of T)
同样的数据库⽂档的更新操作由 UpdateDefinitionBuilder 设置更新字段后产⽣的 UpdateDefinition完成
窗体程序部分
ImportsMongoDB.BsonImportsMongoDB.DriverPublic ClassForm1PublicclientPublic bsdoc AsvbtestPublic collection As IMongoCollection(Ofvbtest)Public database AsIMongoDatabasePublic mlist As List(Ofvbtest)Public builder As FilterDefinitionBuilder(Of vbtest) = New FilterDefinitionBuilder(Ofvbtest)Public vm As vbmongoT(Of vbtest) = New vbmongoT(Of vbtest)("meandmycoach", "vbtest")Public filter As FilterDefinition(Ofvbtest)Private Async Sub
Button1_Click(sender As Object, e As EventArgs) HandlesButton1.Click
vm.filter= vm.builder.Eq(Of ObjectId)("_id", NewObjectId(ComboBox1.Text))
vm.up= New UpdateDefinitionBuilder(Ofvbtest)
vm.updef= vm.up.Set(Of String)("content", t)‘vm.up.Set(Of String)("content", t)
vm.updef =vm.up.Combine(vm.updef)
vm.updateone(vm.filter, vm.updef)End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
vm= New vbmongoT(Of vbtest)("meandmycoach", "vbtest")Dim bsdoc As vbtest = Newvbtest()End Sub
Private Async Sub PictureBox1_Click(sender As Object, e As EventArgs) HandlesPictureBox1.Click
mlist=Await vm.loadrecords()For i As Integer = 0 To mlist.Count -
1ComboBox1.Items.Add(mlist(i)._id.ToString())NextComboBox1.Text= ComboBox1.Items(0)
Label2.Text=mlist.Count.ToString()End Sub
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) HandlesRichTextBox1.TextChangedEnd Sub Private Async Sub ComboBox1_TextChanged(sender As Object, e As EventArgs) HandlesComboBox1.TextChanged
bsdoc=Await vm.loadone(ComboBox1.Text)
RichTextBox1.tEnd Sub
Private Async Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
HandlesComboBox1.SelectedIndexChangedEnd Sub
End Class
mysql下载的vs库放在那个文件里功能描述:通过点击图⽚,加载所有的vbtest⽂档记录,combobox1专门⽤于显⽰ObjectId,可以通过ToString()⽅法转换为普通字符串
随着下拉框ObjectId的改变,richtext1控件加载 vbtest实体类对应记录的Content字段,可以更改字段内容后 点击 更新提交 按钮完成⽂档的UpdateOneAsync操作
vs2019 下⽤ vb窗体程序连接 mongodb4.2
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论