ADO vs. ADO.NET Webservice

2002-9-12 14:34:22【作者】 畅享网 【进入论坛】
本文关键字 理论探讨
广告

ADO vs. ADO.NET Webservice

 

Introduction

In this example I'd like to demonstrate one of the most interesting features of the .NET runtime - the concept of Webservices. My second goal is to demonstrate using the "old" COM ADO 2.6 vs. the ADO.NET. It must be said, that the .NET environment DOES make it possible to use COM components and COM-based technologies, but it's not so straigthforward as using native .NET classes.

This example is built in the beta-1 of the Visual Studio.NET. You need a Windows 2000 IIS/PWS Webserver and SQL Server 7.0/2000 with the well-known Northwind Database in order to run this example.

1. First we create a C# Webservice project and call it "tws" as test-web-service for example. The IDE creates for us a set of files, but we are interested only on the *.asmx and *.cs files. The *.asmx file contains only one line of code:

<%@ WebService Language="c#" Codebehind="tws.cs" Class= "PROINFO.WebService.Data.WS" %>
 
Notice, that the Class= "..." must be identical with the namespace+classname used in your "codebehind" C# code file, otherwise your webservice project won't run. There is a bug in the IDE, because when you rename your namespace or class, this change isn't reflected in the *.asmx file automatically.

2. Further we will work with the C# code file. The IDE created for us all the necessary framework stuff, all we have to do is to write some useful Webmethods - this is a smart name for the class methods we can call through the HTTP/SOAP.

3. Now let's try to write a Webmethod, that will return the Customers table from the Northwind SQL Server database. In the first method GetCustomersOld() we will use the good old ADO 2.6, so we must reference the ActiveX Data Object COM component (project - add reference - COM). The code for creating an ADO connection and a recordset is well-known, and there is no explanation necessary. But the problem is, that as a Webmethod returns everything in XML format, we can't use ADO Recordset as a return type. Instead we must use a structure sCustomers and fill it with all the records found in the table - that isn't very practical. Finally we put the [Webmethod] attribute in front of the method and that's all - the webmethod is perfect and waiting for the first call.

4. In the second method GetCustomersNew() we decide to make the same using ADO.NET instead of ADO 2.6. Not only is this the native .NET technology, but we can simply return the data in ADO.NET DataSet, and the framework transforms it to a nice XML data stream. As you can see, is this method much more simple and there is no need for a COM-to.-NET wrapper.

5. Now we can test the code by running the project. The system will show us a default webbrowser interface for testing the webmethods, and the only we have to do is to push the appropriate "Invoke" button. When everything OK, we will see the result in a separate window as XML data. You can compare the XML format returning from both the methods and the performance.

Source Code

After you create your C# Webservice project, you can paste this code into the C# code file. Don't forget to change the server name, userid and password in the connection parameters of both webmethods.

namespace PROINFO.WebService.Data

{
   
using System;
   
using System.Collections;
   
using System.Configuration;
   
using System.ComponentModel;
   
using System.Data;
   
using System.Data.SQL;
   
using System.Diagnostics;
   
using System.Web;
   
using System.Web.Services;
   
/// <summary>
   
///    Summary description for WS.
   
/// </summary>
   
public class WS : System.Web.Services.WebService
   
{
       
public WS()
       
{
           
//CODEGEN: This call is required by the ASP+ Web Services Designer
           
InitializeComponent();
       
}
       
/// <summary>
       
///    Required method for Designer support - do not modify
       
///    the contents of this method with the code editor.
       
/// </summary>
       
private void InitializeComponent()
       
{
       
}
       
/// <summary>
       
///    Clean up any resources being used.
       
/// </summary>
       
public override void Dispose()
       
{
       
}

 // Here starts the example code
   
public struct sCustomers
   
{
     
public String sCustomerID;
     
public String sCompanyName;
     
public String sContactName;
     
public String sContactTitle;
     
public String sAddress;
     
public String sCity;
     
public String sRegion;
     
public String sPostalCode;
     
public String sCountry;
     
public String sPhone;
     
public String sFax;
    
}

[WebMethod(Description="ADO 2.6 WebMethod Example")]
    
public sCustomers[] GetCustomersOld()
    
{
     
ADODB.Connection cn = new ADODB.Connection();
     
ADODB.Recordset rs = new ADODB.Recordset();
     
String strSQL;
     
int intRC;
     
int intCnt;
     
strSQL = "SELECT * FROM Customers";
     
cn.Open("Provider=SQLOLEDB; Data Source=SERVER; Initial Catalog=Northwind;", "sa", null, 0);
     
rs.Open(strSQL, cn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly, 0);
     
intRC = rs.RecordCount;
     
if (intRC < 1)
     
{
         
return null;
     
}
     
sCustomers[] c = new sCustomers[intRC];
     
rs.MoveFirst();
     
intCnt = 0;

while (!rs.EOF)
     
{
      
c[intCnt].sCustomerID = rs.Fields["CustomerID"].Value.ToString();
      
c[intCnt].sCompanyName = rs.Fields["CompanyName"].Value.ToString();
      
c[intCnt].sContactName = rs.Fields["ContactName"].Value.ToString();
      
c[intCnt].sContactTitle = rs.Fields["ContactTitle"].Value.ToString();
      
c[intCnt].sAddress = rs.Fields["Address"].Value.ToString();
      
c[intCnt].sCity = rs.Fields["City"].Value.ToString();
      
c[intCnt].sRegion = rs.Fields["Region"].Value.ToString();
      
c[intCnt].sPostalCode = rs.Fields["PostalCode"].Value.ToString();
      
c[intCnt].sCountry = rs.Fields["Country"].Value.ToString();
      
c[intCnt].sPhone = rs.Fields["Phone"].Value.ToString();
      
c[intCnt].sFax = rs.Fields["Fax"].Value.ToString();
      
rs.MoveNext();
      
intCnt++;
     
}
     
return c;
     


 [WebMethod(Description="ADO.NET WebMethod Example")]
    
public DataSet GetCustomersNew()
    
{
     
DataSet ds = new DataSet();
     
SQLConnection cn = new SQLConnection("localhost", "sa", "", "Northwind");
     
cn.Open();
     
SQLDataSetCommand cm = new SQLDataSetCommand("SELECT * FROM Customers", cn);
     
cm.FillDataSet(ds, "Customers");
     
return ds;
   
 }
   
}

}

About the Author: Robert Rybaric is an independent software developer and owner of the PRO-INFO system company in Slovakia. He is an MCP in VB. He develops database applications in VB, Access, Office and SQL Server and is a fan of the new C# language. Reach him at pro-fit@pro-fit.sk . More articles...

如果您希望与本文章的作者或其所在机构,进一步交流,请联系:畅享网 姜小姐
jill.jiang@amteam.org | 021-51096826-112 | 在线联系
吴勇毅 专栏CIO 应向刘邦学管理

而国内不少专家也认为,“七分管理,三分技术”,CIO优良与否,与技术出身有关,更与整体素质有关。

夏敬华的KM专栏[原创]智慧的和谐—知识管理推..

从知识管理的角度来观察执行力体系,我们会发现,知识管理和战略、运营和人员这三个环节之间有着内在紧密的逻辑联系。

KM八爪鱼-萧秋水的专栏[原创]企业知识库2.0

面对经济危机,企业更应该关注知识管理,关注知识库的构建,扩充知识储备,提高企业智商和竞争优势。

前沿论丛2009年第三期——知识管理..

国内中小企业普遍存在管理基础薄弱、规范化程度低、信息化基础差等方面的问题,而知识管理的实施难度甚至要高于ERP的实施,因为简单的从上而下压迫式的推行只能做到知识……