看上去这个例子很简单哦,我们建立一个web服务,通过服务,我们查询Northwind.mdb数据库中“客户”表中某客户的地址,并返回该地址,查询条件是客户id,下面我们新建一个asp.net web服务项目,使用默认名称:WebService1,然后,编译一下,我们找到Northwind.mdb这个数据库文件,在“服务器资源浏览器”里面点击“添加链接”,选择“用于 OLE DB 的 .NET Framework 数据提供程序”,然后找到northwind.mdb的文件,添加到项目中来。
然后,把oleDbAdaptar拖到界面,设置好:SELECT 城市, 传真, 地区, 地址, 电话, 公司名称, 国家, 客户ID, 联系人头衔, 联系人姓名, 邮政编码 FROM 客户
然后自动回生成oldDbConnection,在拖入一个DataSet空件,设置为“非类型化数据集”。
然后,就可以添加代码了,代码如下:
主要Code
[WebMethod]
public string GetCustomerAddress(string CustomerID)
{//查询客户地址
string StrAddress = "";
try
{
string StrSQL = "SELECT * FROM 客户 WHERE 客户ID LIKE '";
StrSQL += CustomerID + "'";
this.oleDbDataAdapter1.SelectCommand.CommandText = StrSQL;
this.oleDbDataAdapter1.SelectCommand.Connection = this.oleDbConnection1;
this.oleDbConnection1.Open();
this.oleDbDataAdapter1.SelectCommand.ExecuteNonQuery();
this.oleDbConnection1.Close();
//更新表中的数据
this.dataSet1.Clear();
this.oleDbDataAdapter1.Fill(this.dataSet1);
StrAddress = (string)this.dataSet1.Tables["客户"].Rows[0].ItemArray.GetValue(4);
if (StrAddress.Length > 0)
{
StrAddress = CustomerID + "客户的地址是:" + StrAddress;
}
}
catch (Exception Err)
{
StrAddress = "没有查询到" + CustomerID + "客户的地址" ;
}
return StrAddress;
}
[WebMethod]
public string GetCustomerAddress(string CustomerID)
{//查询客户地址
string StrAddress = "";
try
{
string StrSQL = "SELECT * FROM 客户 WHERE 客户ID LIKE '";
StrSQL += CustomerID + "'";
this.oleDbDataAdapter1.SelectCommand.CommandText = StrSQL;
this.oleDbDataAdapter1.SelectCommand.Connection = this.oleDbConnection1;
this.oleDbConnection1.Open();
this.oleDbDataAdapter1.SelectCommand.ExecuteNonQuery();
this.oleDbConnection1.Close();
//更新表中的数据
this.dataSet1.Clear();
this.oleDbDataAdapter1.Fill(this.dataSet1);
StrAddress = (string)this.dataSet1.Tables["客户"].Rows[0].ItemArray.GetValue(4);
if (StrAddress.Length > 0)
{
StrAddress = CustomerID + "客户的地址是:" + StrAddress;
}
}
catch (Exception Err)
{
StrAddress = "没有查询到" + CustomerID + "客户的地址" ;
}
return StrAddress;
}
完整Code
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace WebService1
{
/// <summary>
/// Service1 的摘要说明。
/// </summary>
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
private System.Data.DataSet dataSet1;
#region 组件设计器生成的代码
//Web 服务设计器所必需的
private IContainer components = null;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.dataSet1 = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
//
// oleDbSelectCommand1
//
this.oleDbSelectCommand1.CommandText = "SELECT 城市, 传真, 地区, 地址, 电话, 公司名称, 国家, 客户ID, 联系人头衔, 联系人姓名, 邮政编码 FROM 客户";
this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Northwind.mdb;Persis" +
"t Security Info=False";
//
// oleDbInsertCommand1
//
this.oleDbInsertCommand1.CommandText = "INSERT INTO `客户` (`城市`, `传真`, `地区`, `地址`, `电话`, `公司名称`, `国家`, `客户ID`, `联系人头衔`, `联" +
"系人姓名`, `邮政编码`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
this.oleDbInsertCommand1.Parameters.AddRange(new System.Data.OleDb.OleDbParameter[] {
new System.Data.OleDb.OleDbParameter("城市", System.Data.OleDb.OleDbType.VarWChar, 0, "城市"),
new System.Data.OleDb.OleDbParameter("传真", System.Data.OleDb.OleDbType.VarWChar, 0, "传真"),
new System.Data.OleDb.OleDbParameter("地区", System.Data.OleDb.OleDbType.VarWChar, 0, "地区"),
new System.Data.OleDb.OleDbParameter("地址", System.Data.OleDb.OleDbType.VarWChar, 0, "地址"),
new System.Data.OleDb.OleDbParameter("电话", System.Data.OleDb.OleDbType.VarWChar, 0, "电话"),
new System.Data.OleDb.OleDbParameter("公司名称", System.Data.OleDb.OleDbType.VarWChar, 0, "公司名称"),
new System.Data.OleDb.OleDbParameter("国家", System.Data.OleDb.OleDbType.VarWChar, 0, "国家"),
new System.Data.OleDb.OleDbParameter("客户ID", System.Data.OleDb.OleDbType.VarWChar, 0, "客户ID"),
new System.Data.OleDb.OleDbParameter("联系人头衔", System.Data.OleDb.OleDbType.VarWChar, 0, "联系人头衔"),
new System.Data.OleDb.OleDbParameter("联系人姓名", System.Data.OleDb.OleDbType.VarWChar, 0, "联系人姓名"),
new System.Data.OleDb.OleDbParameter("邮政编码", System.Data.OleDb.OleDbType.VarWChar, 0, "邮政编码")});
//
// oleDbUpdateCommand1
//
this.oleDbUpdateCommand1.CommandText = "UPDATE `客户` SET `城市` = ?, `传真` = ?, `地区` = ?, `地址` = ?, `电话` = ?, `公司名称` = ?, `国家" +
"` = ?, `客户ID` = ?, `联系人头衔` = ?, `联系人姓名` = ?, `邮政编码` = ? WHERE ((`客户ID` = ?))";
this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
this.oleDbUpdateCommand1.Parameters.AddRange(new System.Data.OleDb.OleDbParameter[] {
new System.Data.OleDb.OleDbParameter("城市", System.Data.OleDb.OleDbType.VarWChar, 0, "城市"),
new System.Data.OleDb.OleDbParameter("传真", System.Data.OleDb.OleDbType.VarWChar, 0, "传真"),
new System.Data.OleDb.OleDbParameter("地区", System.Data.OleDb.OleDbType.VarWChar, 0, "地区"),
new System.Data.OleDb.OleDbParameter("地址", System.Data.OleDb.OleDbType.VarWChar, 0, "地址"),
new System.Data.OleDb.OleDbParameter("电话", System.Data.OleDb.OleDbType.VarWChar, 0, "电话"),
new System.Data.OleDb.OleDbParameter("公司名称", System.Data.OleDb.OleDbType.VarWChar, 0, "公司名称"),
new System.Data.OleDb.OleDbParameter("国家", System.Data.OleDb.OleDbType.VarWChar, 0, "国家"),
new System.Data.OleDb.OleDbParameter("客户ID", System.Data.OleDb.OleDbType.VarWChar, 0, "客户ID"),
new System.Data.OleDb.OleDbParameter("联系人头衔", System.Data.OleDb.OleDbType.VarWChar, 0, "联系人头衔"),
new System.Data.OleDb.OleDbParameter("联系人姓名", System.Data.OleDb.OleDbType.VarWChar, 0, "联系人姓名"),
new System.Data.OleDb.OleDbParameter("邮政编码", System.Data.OleDb.OleDbType.VarWChar, 0, "邮政编码"),
new System.Data.OleDb.OleDbParameter("Original_客户ID", System.Data.OleDb.OleDbType.VarWChar, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "客户ID", System.Data.DataRowVersion.Original, null)});
//
// oleDbDeleteCommand1
//
this.oleDbDeleteCommand1.CommandText = "DELETE FROM `客户` WHERE ((`客户ID` = ?))";
this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;
this.oleDbDeleteCommand1.Parameters.AddRange(new System.Data.OleDb.OleDbParameter[] {
new System.Data.OleDb.OleDbParameter("Original_客户ID", System.Data.OleDb.OleDbType.VarWChar, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "客户ID", System.Data.DataRowVersion.Original, null)});
//
// oleDbDataAdapter1
//
this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "客户", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("城市", "城市"),
new System.Data.Common.DataColumnMapping("传真", "传真"),
new System.Data.Common.DataColumnMapping("地区", "地区"),
new System.Data.Common.DataColumnMapping("地址", "地址"),
new System.Data.Common.DataColumnMapping("电话", "电话"),
new System.Data.Common.DataColumnMapping("公司名称", "公司名称"),
new System.Data.Common.DataColumnMapping("国家", "国家"),
new System.Data.Common.DataColumnMapping("客户ID", "客户ID"),
new System.Data.Common.DataColumnMapping("联系人头衔", "联系人头衔"),
new System.Data.Common.DataColumnMapping("联系人姓名", "联系人姓名"),
new System.Data.Common.DataColumnMapping("邮政编码", "邮政编码")})});
this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
//
// dataSet1
//
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// WEB 服务示例
// HelloWorld() 示例服务返回字符串 Hello World
// 若要生成,请取消注释下列行,然后保存并生成项目
// 若要测试此 Web 服务,请按 F5 键
[WebMethod]
public string HelloWorld()
{
return "中华人民共和国";
}
[WebMethod]
public string GetCustomerAddress(string CustomerID)
{//查询客户地址
string StrAddress = "";
try
{
string StrSQL = "SELECT * FROM 客户 WHERE 客户ID LIKE '";
StrSQL += CustomerID + "'";
this.oleDbDataAdapter1.SelectCommand.CommandText = StrSQL;
this.oleDbDataAdapter1.SelectCommand.Connection = this.oleDbConnection1;
this.oleDbConnection1.Open();
this.oleDbDataAdapter1.SelectCommand.ExecuteNonQuery();
this.oleDbConnection1.Close();
//更新表中的数据
this.dataSet1.Clear();
this.oleDbDataAdapter1.Fill(this.dataSet1);
StrAddress = (string)this.dataSet1.Tables["客户"].Rows[0].ItemArray.GetValue(4);
if (StrAddress.Length > 0)
{
StrAddress = CustomerID + "客户的地址是:" + StrAddress;
}
}
catch (Exception Err)
{
StrAddress = "没有查询到" + CustomerID + "客户的地址" ;
}
return StrAddress;
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace WebService1
{
/// <summary>
/// Service1 的摘要说明。
/// </summary>
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
private System.Data.DataSet dataSet1;
#region 组件设计器生成的代码
//Web 服务设计器所必需的
private IContainer components = null;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.dataSet1 = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
//
// oleDbSelectCommand1
//
this.oleDbSelectCommand1.CommandText = "SELECT 城市, 传真, 地区, 地址, 电话, 公司名称, 国家, 客户ID, 联系人头衔, 联系人姓名, 邮政编码 FROM 客户";
this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Northwind.mdb;Persis" +
"t Security Info=False";
//
// oleDbInsertCommand1
//
this.oleDbInsertCommand1.CommandText = "INSERT INTO `客户` (`城市`, `传真`, `地区`, `地址`, `电话`, `公司名称`, `国家`, `客户ID`, `联系人头衔`, `联" +
"系人姓名`, `邮政编码`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
this.oleDbInsertCommand1.Parameters.AddRange(new System.Data.OleDb.OleDbParameter[] {
new System.Data.OleDb.OleDbParameter("城市", System.Data.OleDb.OleDbType.VarWChar, 0, "城市"),
new System.Data.OleDb.OleDbParameter("传真", System.Data.OleDb.OleDbType.VarWChar, 0, "传真"),
new System.Data.OleDb.OleDbParameter("地区", System.Data.OleDb.OleDbType.VarWChar, 0, "地区"),
new System.Data.OleDb.OleDbParameter("地址", System.Data.OleDb.OleDbType.VarWChar, 0, "地址"),
new System.Data.OleDb.OleDbParameter("电话", System.Data.OleDb.OleDbType.VarWChar, 0, "电话"),
new System.Data.OleDb.OleDbParameter("公司名称", System.Data.OleDb.OleDbType.VarWChar, 0, "公司名称"),
new System.Data.OleDb.OleDbParameter("国家", System.Data.OleDb.OleDbType.VarWChar, 0, "国家"),
new System.Data.OleDb.OleDbParameter("客户ID", System.Data.OleDb.OleDbType.VarWChar, 0, "客户ID"),
new System.Data.OleDb.OleDbParameter("联系人头衔", System.Data.OleDb.OleDbType.VarWChar, 0, "联系人头衔"),
new System.Data.OleDb.OleDbParameter("联系人姓名", System.Data.OleDb.OleDbType.VarWChar, 0, "联系人姓名"),
new System.Data.OleDb.OleDbParameter("邮政编码", System.Data.OleDb.OleDbType.VarWChar, 0, "邮政编码")});
//
// oleDbUpdateCommand1
//
this.oleDbUpdateCommand1.CommandText = "UPDATE `客户` SET `城市` = ?, `传真` = ?, `地区` = ?, `地址` = ?, `电话` = ?, `公司名称` = ?, `国家" +
"` = ?, `客户ID` = ?, `联系人头衔` = ?, `联系人姓名` = ?, `邮政编码` = ? WHERE ((`客户ID` = ?))";
this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
this.oleDbUpdateCommand1.Parameters.AddRange(new System.Data.OleDb.OleDbParameter[] {
new System.Data.OleDb.OleDbParameter("城市", System.Data.OleDb.OleDbType.VarWChar, 0, "城市"),
new System.Data.OleDb.OleDbParameter("传真", System.Data.OleDb.OleDbType.VarWChar, 0, "传真"),
new System.Data.OleDb.OleDbParameter("地区", System.Data.OleDb.OleDbType.VarWChar, 0, "地区"),
new System.Data.OleDb.OleDbParameter("地址", System.Data.OleDb.OleDbType.VarWChar, 0, "地址"),
new System.Data.OleDb.OleDbParameter("电话", System.Data.OleDb.OleDbType.VarWChar, 0, "电话"),
new System.Data.OleDb.OleDbParameter("公司名称", System.Data.OleDb.OleDbType.VarWChar, 0, "公司名称"),
new System.Data.OleDb.OleDbParameter("国家", System.Data.OleDb.OleDbType.VarWChar, 0, "国家"),
new System.Data.OleDb.OleDbParameter("客户ID", System.Data.OleDb.OleDbType.VarWChar, 0, "客户ID"),
new System.Data.OleDb.OleDbParameter("联系人头衔", System.Data.OleDb.OleDbType.VarWChar, 0, "联系人头衔"),
new System.Data.OleDb.OleDbParameter("联系人姓名", System.Data.OleDb.OleDbType.VarWChar, 0, "联系人姓名"),
new System.Data.OleDb.OleDbParameter("邮政编码", System.Data.OleDb.OleDbType.VarWChar, 0, "邮政编码"),
new System.Data.OleDb.OleDbParameter("Original_客户ID", System.Data.OleDb.OleDbType.VarWChar, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "客户ID", System.Data.DataRowVersion.Original, null)});
//
// oleDbDeleteCommand1
//
this.oleDbDeleteCommand1.CommandText = "DELETE FROM `客户` WHERE ((`客户ID` = ?))";
this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;
this.oleDbDeleteCommand1.Parameters.AddRange(new System.Data.OleDb.OleDbParameter[] {
new System.Data.OleDb.OleDbParameter("Original_客户ID", System.Data.OleDb.OleDbType.VarWChar, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "客户ID", System.Data.DataRowVersion.Original, null)});
//
// oleDbDataAdapter1
//
this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "客户", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("城市", "城市"),
new System.Data.Common.DataColumnMapping("传真", "传真"),
new System.Data.Common.DataColumnMapping("地区", "地区"),
new System.Data.Common.DataColumnMapping("地址", "地址"),
new System.Data.Common.DataColumnMapping("电话", "电话"),
new System.Data.Common.DataColumnMapping("公司名称", "公司名称"),
new System.Data.Common.DataColumnMapping("国家", "国家"),
new System.Data.Common.DataColumnMapping("客户ID", "客户ID"),
new System.Data.Common.DataColumnMapping("联系人头衔", "联系人头衔"),
new System.Data.Common.DataColumnMapping("联系人姓名", "联系人姓名"),
new System.Data.Common.DataColumnMapping("邮政编码", "邮政编码")})});
this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
//
// dataSet1
//
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// WEB 服务示例
// HelloWorld() 示例服务返回字符串 Hello World
// 若要生成,请取消注释下列行,然后保存并生成项目
// 若要测试此 Web 服务,请按 F5 键
[WebMethod]
public string HelloWorld()
{
return "中华人民共和国";
}
[WebMethod]
public string GetCustomerAddress(string CustomerID)
{//查询客户地址
string StrAddress = "";
try
{
string StrSQL = "SELECT * FROM 客户 WHERE 客户ID LIKE '";
StrSQL += CustomerID + "'";
this.oleDbDataAdapter1.SelectCommand.CommandText = StrSQL;
this.oleDbDataAdapter1.SelectCommand.Connection = this.oleDbConnection1;
this.oleDbConnection1.Open();
this.oleDbDataAdapter1.SelectCommand.ExecuteNonQuery();
this.oleDbConnection1.Close();
//更新表中的数据
this.dataSet1.Clear();
this.oleDbDataAdapter1.Fill(this.dataSet1);
StrAddress = (string)this.dataSet1.Tables["客户"].Rows[0].ItemArray.GetValue(4);
if (StrAddress.Length > 0)
{
StrAddress = CustomerID + "客户的地址是:" + StrAddress;
}
}
catch (Exception Err)
{
StrAddress = "没有查询到" + CustomerID + "客户的地址" ;
}
return StrAddress;
}
}
}
编译后即可。如下图的调试界面:
点击GetCustomerAddress,后出现如下图片:
我们输入ALfki,得到查询结果。 <?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">alfki客户的地址是:大崇明路 50 号</string>