using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
namespace ResinService
{
/// <summary>
/// Service1 的摘要说明。
/// </summary>
public class ResinService : System.Web.Services.WebService
{
public ResinService()
{
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}
#region 组件设计器生成的代码
//Web 服务设计器所必需的
private IContainer components = null;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
/// <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 "Hello World";
// }
[WebMethod]
public double WebAdd(double db1,double db2)
{
return db1+db2;
}
[WebMethod]
public DataSet SQLDB(string Query)
{
try
{
SqlConnection CS = new SqlConnection("server=.;uid=sa;pwd=13584002996;database=SUNPOS");
SqlDataAdapter myCommand = new SqlDataAdapter (Query, CS);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "Results");
return myDataSet;
}
catch(Exception ex)
{
return DataError(ex);
}
}
[WebMethod]
public string GetStock(DataSet NetDate)
{
//取dataset的内容ds.Tables["Suppliers"]
string CompnayName=NetDate.Tables["Suppliers"].Rows[0]["CompanyName"].ToString();
//可通过访问数据行来更改数据行中的数据。可以使用行集合中的行索引,该行索引通过 Rows 属性来访问:
//myDataSet.Tables["Customers"].Rows[0]["ContactName"]="Peach";
return CompnayName;
}
public DataSet DataError(Exception ex)
{
DataSet errDS = new DataSet("Errors");
DataTable errTable = errDS.Tables.Add("Error");
errTable.Columns.Add("Message");
errTable.Rows.Add(new Object[] {ex.Message});
return errDS;
}
}
}
2.
生成代理类由本地使用
D:\temp>WSDL /out:ResinService.cs http://hhq80.vicp.net/ResinService/Service1.asmx
D:\temp>csc /out:ResinService.dll /t:library /r:system.xml.dll /r:system.web.ser
vices.dll ResinService.cs
3.在vs中加入引用
4.建立应用程序,并调用代理,得到数据
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace UseResinService
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.DataGrid myGrid;
private System.Windows.Forms.Button LocalDB;
private DataSet ds;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.myGrid = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.LocalDB = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.myGrid)).BeginInit();
this.SuspendLayout();
//
// myGrid
//
this.myGrid.DataMember = "";
this.myGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.myGrid.Location = new System.Drawing.Point(8, 0);
this.myGrid.Name = "myGrid";
this.myGrid.Size = new System.Drawing.Size(568, 328);
this.myGrid.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(400, 336);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "提取服务器";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(112, 336);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "上传数据";
//
// LocalDB
//
this.LocalDB.Location = new System.Drawing.Point(24, 336);
this.LocalDB.Name = "LocalDB";
this.LocalDB.TabIndex = 3;
this.LocalDB.Text = "提到本地数据";
this.LocalDB.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(584, 365);
this.Controls.Add(this.LocalDB);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.myGrid);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.myGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
ResinService rs=new ResinService();
string goods="select top 60 gbm,ypm,gg,cd,zxdw,lsj from zy_sys2_ypzdk";
DataSet ds=rs.SQLDB(goods);
myGrid.DataSource=ds;
myGrid.Visible=true;
myGrid.AllowSorting=true;
}
private void button3_Click(object sender, System.EventArgs e)
{
ConnectToData();
myGrid.SetDataBinding(ds, "Suppliers");
ResinService rs=new ResinService();
string result=rs.GetStock(ds);
MessageBox.Show(this,result);
}
void ConnectToData()
{
// Create the ConnectionString and create a SqlConnection.
// Change the data source value to the name of your computer.
string cString = "server=.;uid=sa;pwd=13584002996;database=northwind";
SqlConnection myConnection = new SqlConnection(cString);
// Create a SqlDataAdapter.
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.TableMappings.Add("Table", "Suppliers");
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM Suppliers",myConnection);
myCommand.CommandType = CommandType.Text;
myAdapter.SelectCommand = myCommand;
Console.WriteLine("The connection is open");
ds = new DataSet("Customers");
myAdapter.Fill(ds);
// Create a second Adapter and Command.
SqlDataAdapter adpProducts = new SqlDataAdapter();
adpProducts.TableMappings.Add("Table", "Products");
SqlCommand cmdProducts = new SqlCommand("SELECT * FROM Products", myConnection);
adpProducts.SelectCommand = cmdProducts;
adpProducts.Fill(ds);
myConnection.Close();
Console.WriteLine("The connection is closed.");
System.Data.DataRelation dr;
System.Data.DataColumn dc1;
System.Data.DataColumn dc2;
// Get the parent and child columns of the two tables.
dc1 = ds.Tables["Suppliers"].Columns["SupplierID"];
dc2 = ds.Tables["Products"].Columns["SupplierID"];
dr = new System.Data.DataRelation("suppliers2products", dc1, dc2);
ds.Relations.Add(dr);
}
}
}