當(dāng)前位置:工程項(xiàng)目OA系統(tǒng) > 泛普各地 > 河北O(jiān)A系統(tǒng) > 石家莊OA系統(tǒng) > 石家莊OA信息化
ADO vs. ADO.NET Webservice
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...
- 1透視Best Buy石家莊OA信息化實(shí)踐(by AMT 夏敬華 編譯)
- 2使用Visual Studio.Net建立web service
- 3石家莊OA信息化隨筆之一:石家莊OA信息化“突圍”(by AMT 夏敬華)
- 4Web Service Case Study: 認(rèn)證考試申請服務(wù)
- 5石家莊OA信息化的“三四五六七”(by AMT 石家莊OA信息化小組)
- 6Web服務(wù)網(wǎng)絡(luò):簡化企業(yè)間工程的中介
- 7實(shí)用工具:IT員工的培訓(xùn)補(bǔ)償政策示例
- 8Managing Knowledge Workers
- 9OA辦公系統(tǒng)的信息發(fā)布與管理門戶介紹
- 10什么是真正的石家莊OA信息化
- 11對某集團(tuán)公司協(xié)同辦公系統(tǒng)未來3-5年的IT規(guī)劃建設(shè)藍(lán)圖
- 12搜索:非結(jié)構(gòu)化信息管理的核心
- 13xml and KM
- 14面向服務(wù)的應(yīng)用集成——EAI和Web服務(wù)
- 15不僅看投資回報(bào),還要看“知識(shí)回報(bào)”
- 16At Your Service, On the Web
- 17Web服務(wù)內(nèi)幕,第10部分:深入主題:可靠性和事務(wù)
- 18微軟:“Web服務(wù)我們領(lǐng)先Sun 18個(gè)月”
- 19TIBCO來華布道Web服務(wù)戰(zhàn)略
- 20微軟將于10月采取訂閱銷售模式
- 21SOAP技術(shù)與B2B應(yīng)用集成--SOAP的型系統(tǒng)和數(shù)據(jù)編碼規(guī)則
- 22ITToolBox e-Business(by AMT整理)
- 23石家莊OA信息化的基本XML和RDF技術(shù)(二):將文件合并到RDF模型和基本的RDF查詢
- 24IBM為Web服務(wù)安全 發(fā)布一系列有爭議的API
- 25[編譯] 石家莊OA信息化測度:目標(biāo)、過程及方法(夏敬華譯)
- 26企業(yè)CIO剖析中小企業(yè)信息化發(fā)展建設(shè)盲點(diǎn).
- 27EIP 相關(guān)資源
- 28泛普OA軟件支持在線直接發(fā)送消息、傳送文件、音頻會(huì)話等
- 29協(xié)同之惑
- 30[理論] 如何根據(jù)業(yè)務(wù)過程選擇知識(shí)應(yīng)用模式?(夏敬華)
成都公司:成都市成華區(qū)建設(shè)南路160號(hào)1層9號(hào)
重慶公司:重慶市江北區(qū)紅旗河溝華創(chuàng)商務(wù)大廈18樓
版權(quán)所有:泛普軟件 渝ICP備14008431號(hào)-2 渝公網(wǎng)安備50011202501700號(hào) 咨詢電話:400-8352-114