vb.net - Connect to MongoDB server with Driver 2.0 best practice -


i have vb.net code connects mongodb. when database , running code works fine, when database not running don't errors back.

how check server , running can connect , work? if server work else return message user server not available.

i @ documentation mongoclient class can't seem find can use.

mongoclient class (http://api.mongodb.org/csharp/2.0/html/t_mongodb_driver_mongoclient.htm)

below code works connect mongodb:

public function dbconnection(byref connstring string, vdbname string, vcolname string) mongoclient      'default port     'connstring = "mongodb://localhost:27017"      'example db , collection     'vdbname = "blog"     'vcolname = "users"      'root object     dim vclient mongoclient     vclient = new mongoclient(connstring)      dim vdb mongodatabasebase     vdb = vclient.getdatabase(vdbname)      dim vcol imongocollection(of bsondocument)     vcol = vdb.getcollection(of bsondocument)(vcolname)      return vclient  end function 

below additional code use insertoneasync without creating error:

private async sub button1_click(sender object, e eventargs) handles button1.click     if txtname.text = ""         msgbox("enter name database.")     else          dim connstring string         connstring = txtconnstr.text          dim vdbname string         vdbname = txtdb.text          dim vcolname string         vcolname = txtcoll.text          'root object         dim vclient mongoclient          vclient = dbconnection(connstring, vdbname, vcolname)          dim vdb mongodatabasebase         vdb = vclient.getdatabase(vdbname)          dim vcol imongocollection(of bsondocument)         vcol = vdb.getcollection(of bsondocument)(vcolname)          dim vadduser bsondocument         vadduser = new bsondocument          vadduser.add("_id", txtid.text)         vadduser.add("name ", txtname.text)         vadduser.add("email", txtemail.text)         vadduser.add("city", txtcity.text)          rtfdatadisplay.text = "bsondocument = " & vadduser.tostring & ", #" & vadduser.count          await vcol.insertoneasync(vadduser)      end if end sub 

below solution came with. posting try...catch since have posted entire procedures above.

        try              dim watch stopwatch = new stopwatch              watch.start()              dim insertresult task = vcol.insertoneasync(vadduser)             await insertresult              watch.stop()              msgbox("faulted =" & insertresult.isfaulted.tostring & ", status = " & insertresult.status.tostring & ", watch = " & watch.elapsed.tostring)          catch ex exception             if ex.hresult.tostring = "-2146233083"                  msgbox("unable insert data due timeout exception")             else                 msgbox("unable insert data = " & ", hresult = " & ex.hresult.tostring & "!" & ex.tostring)             end if          end try 

since asynch returns task, doesn't wait until operation complete. if wait after task , capture exception , process accordingly, here sample

change await vcol.insertoneasync(vadduser)

var inserttask = vcol.insertoneasync(vadduser); inserttask.wait(); 

and remove async keyword button_click method signature.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -