用WEB SERVICE 返回数据集的简单应用

1 新建一个ASP.NET Web 服务
在Service1.asmx.cs添加如下代码
[WebMethod]
  public DataSet GetDS(string strSql,string strConn)
  {  
    SqlConnection myConn=new SqlConnection(strConn);
    myConn.Open();
    SqlDataAdapter myAdapter=new SqlDataAdapter(strSql,myConn);
    DataSet myDS=new DataSet();
    myAdapter.Fill(myDS);
    myConn.Close();
    return myDS;
  }
启动之

并输入连接字符串和查询语句,按调用
则返回了一个XML格式的数据集

如果想在客户端的dataGrid中显示出来
则要添加WEB 引用,并using 刚才所建立的SERVICE的命名空间
      string strSql="";
      string strConn="";
     DataSet ds1=new DataSet();
    Service1 myService=new Service1();
    ds1=myService.GetDS(strConn,strSql);
     DataGrid1.DataSource=ds1;
    DataGrid1.DataBind();
posted @ 2006-02-21 13:41  Aldebaran's Home  阅读(1093)  评论(0编辑  收藏  举报