Add perspective: Converts a DataSet returned by Ado.net or WebService to a adodb.recordset

Source: Internet
Author: User
Tags dotnet soap zip
ado|web| conversion Remember in the article "Perspective and adjust your business and business system", we discussed some of the issues with components, SOAP, and webservice, especially for some of these issues:

How to build a simple three-layer structure (Window DNA)

How to expose existing components and become a WebService

How to use MS SOAP Toolkit to generate client consumption webservice

How to invoke the SOAP Toolkit generated WebService in dotnet

How to deal with and transmit adodb.recordset type of data, to achieve their own CTM.

How to encapsulate the current component with ASP.net into webservice

With some discussion and experimentation, I skipped the question of how to receive webservice transmission of data from the group when we discussed the ASP.net encapsulation of our components into WebService and then consumed WebService using MS SOAP Toolkit.



Remember that the code is like this:

Dim Retxml as Object

Dim SoapClient as Mssoaplib.soapclient

Set soapclient = New mssoaplib.soapclient



Call Soapclient.mssoapinit ("Http://Dereksvr/Authors/Authors.asmx?WSDL")



Set retxml = soapclient.getauthors ()

GetAuthors () Returns a dataset type, which we encapsulate when we start generating WebService:

<webmethod () > Public Function getauthors () as DataSet



Dim obj as Bus_authors.authors

Dim rst as ADODB. Recordset



Dim MyDataAdapter as Oledb.oledbdataadapter

Dim Retdataset as DataSet



obj = New bus_authors.authors ()

rst = New ADODB. Recordset ()



MyDataAdapter = New Oledb.oledbdataadapter ()

Retdataset = New DataSet ()



rst = obj. GetAuthors ()

Mydataadapter.fill (Retdataset, RST, "GetAuthors")

GetAuthors = Retdataset

End Function

And for VB Retxml will be can not be recognized and directly used, but the dataset is based on XML, in fact it is regular, we can directly access the. asmx file (http://Dereksvr/Authors/ Authors.asmx) to invoke this WebService GetAuthors () on the Web page, we can see the structure of the dataset in IE so that we can find the rules to use the data in this dataset.










Based on the above, I wrote a function that converts a dataset to Adodb.recordset.

Public Function Convdatasettorecordset (ByVal vonl as IXMLDOMNodeList, ByVal Vstablename as String) as ADODB. Recordset



Dim Ixmltablenode as IXMLDOMNode

Dim Ixmlrecordnode as IXMLDOMNode

Dim Ixmlfieldnode as IXMLDOMNode

Dim Ixmlnodelist as IXMLDOMNodeList



Dim Retrs as ADODB. Recordset

Dim Sxpath as String



On Error GoTo Errhandle



The ' Create Recordset using the ' XSD schema

Sxpath = "//xsd:element[@name =" "& Vstablename &" "]/xsd:complextype/xsd:sequence"

Set Ixmltablenode = Vonl.item (1). selectSingleNode (Sxpath)



Set retrs = New ADODB. Recordset



For each ixmlfieldnode in Ixmltablenode.childnodes

If not ixmlfieldnode.attributes are nothing Then

Call RetRS.Fields.Append (ixmlfieldnode.attributes (0). Text, Getdatatype (ixmlfieldnode.attributes (1). Text), 512)

End If

Next



' Add the data to the Recordset

Sxpath = "//" & Vstablename

Set ixmlnodelist = Vonl.item (3). selectnodes (Sxpath)



Call Retrs.open



For each ixmlrecordnode in Ixmlnodelist

Call Retrs.addnew

For each ixmlfieldnode in Ixmlrecordnode.childnodes

If Len (Ixmlfieldnode.basename) > 0 Then

Retrs.fields (ixmlfieldnode.basename) = Ixmlfieldnode.text

End If

Next

Next



If not (RETRS.BOF and retrs.eof) Then call Retrs.movefirst

Set Convdatasettorecordset = Retrs



Errexit:

Exit Function



Errhandle:

Set Convdatasettorecordset = Nothing

Resume Errexit



End Function



Private Function Getdatatype (ByVal Vstype as String) as ADODB. DataTypeEnum

' Convert the XSD datatype to a ADO datatype

Select Case Vstype

Case "Xsd:string"

Getdatatype = adVarChar

Case "Xsd:int"

Getdatatype = Adinteger

Case "Xsd:datetime"

Getdatatype = Addate

Case "Xsd:decimal"

Getdatatype = addouble

Case "Xsd:boolean"

Getdatatype = Adboolean

End Select

End Function

I haven't tried all of the types in the Getdatatype, only used a few common ones, see the links below:

Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmrefsupporteddatatypeconversions.asp

Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdcstdatatypeenum.asp

Continue to add to ensure that you can meet your specific needs. And then apply the function of our presentation layer in the third article to display in a grid:

Set retxml = soapclient.getauthors ()

Set result = Convdatasettorecordset (Retxml, "GetAuthors")

Lvwheadname Lstauthors, Strheaders

ADOFILLLVW result, lstauthors



It's interesting to think about it, starting with the Adodb.recordset type, then switching to the dataset type in WebService and then converting to adodb.recordset. XML is a powerful medium, and dotnet is a breakthrough for datasets over the previous version of the Recordset. The application of the dataset will also be extremely flexible and unrestricted, as its core and base is XML.




Since it is an update, I should also request that the project and code for the example referred to in the perspective and adjustment article be uploaded to Csdn, while the other one is a screenshot of the picture, hoping both for reading and understanding will have a little help, the specific installation process I omitted.





Related documents:

Http://263.csdn.net/FileBBS/files/2001_10/T_724_1.zip (Code)

Http://263.csdn.net/FileBBS/files/2001_10/T_724_2.zip (JPG)




--------------------------------------------------------------------------------


Special:



The above text and pictures are related to other people's privacy and personal rights, if not authorized or by my consent, any website or periodical, please do not publish, reprint, adapt, paste or other forms of dissemination. All of the above text and pictures are for internal communication only, not for any press release or commercial use.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.