ADO 2.6 vs. the ADO.Net
在本例中我們需要IIS5環境、Visual Studio.Net BETA1、還有SQL Server中的Northwind數據庫
在.NET中,保持了對早先COM及基於COM技術的良好支持,在本例中提供了兩種方法:GetCustomersOld() 使用了ADO2.6;GetCustomersNew() 使用ADO.Net,可以對比。
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 description for WS.
///
public class WS : System.Web.Services.WebService
{
public WS()
{
//CODEGEN: This call is required by the ASP+ Web Services Designer
InitializeComponent();
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
}
///
/// Clean up any resources being used.
///
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;
}
}
}