Flex调用webService读取SQL数据库

来源:互联网 发布:mac 上打开exe 编辑:程序博客网 时间:2024/06/05 04:59

算不上是原创,而是根据别人的结合自己修改的,学习之用

webService的方法

 [WebMethod]
          public  DataTable func()
        {
            String strconn = "Server=本人的ip或者是.;database=XC0505;user=sa;password=123";
            SqlConnection cn = new SqlConnection(strconn);
            //cn.Open();
            //SqlCommand cm = new SqlCommand("select * from TRUEMAP_CROSSING", cn);
            //SqlDataReader dr = cm.ExecuteReader(); 

            //dg.DataSource = dr;            如果只是在aspx中显示,这样就行,跟Flex中绑定不同
            //dg.DataBind();
            //cn.Close();
           

 

            //  SqlDataAdapter sda = new SqlDataAdapter("select * from TRUEMAP_CROSSING", cn);
            //DataSet ds = new DataSet();
            //sda.Fill(ds);
            //DataTable table = ds.Tables[0];
            //dg.DataSource = table;
            //dg.DataBind();                                               //如果只是在aspx中显示这样也行

 

            SqlDataAdapter sda = new SqlDataAdapter("select * from TRUEMAP_CROSSING", cn);         //这句以后是将查询的数据作为一个表来返回,c#的跟Flex是有区别的
            DataSet ds = new DataSet();      
            sda.Fill(ds);
            DataTable table = ds.Tables[0];
            return table;

      }

Flex端的代码:

   public function init():void
   {
//    myWebservice.addEventListener(ResultEvent.RESULT,onSuccess);
//    myWebservice.addEventListener(FaultEvent.FAULT,onFault);
      this.myWebservice.func();              //func为服务端方法名称
   }
   public function onSuccess(event:ResultEvent):void                       //调用成功执行的方法
   {
    /* dg.dataProvider=this.myWebservice.func.lastResult.Tables.Table.Rows; */                  //这句也可以写在下面   
   }
   public function onDefault(event:FaultEvent):void   
   {
    Alert.show("Error");
    
   }
  ]]>
 </fx:Script>
 <s:layout>
  <s:BasicLayout/>
 </s:layout>
 <fx:Declarations>
  <!-- 将非可视元素(例如服务、值对象)放在此处 -->
  <s:WebService id="myWebservice" wsdl="http://xxx/gaga/Service1.asmx?WSDL"> 
  </s:WebService>
 </fx:Declarations>
 
 <mx:DataGrid id="dg" width="100%" height="100%" dataProvider="{this.myWebservice.func.lastResult.Tables.Table.Rows}">
  <mx:columns>
   <mx:DataGridColumn headerText="LinkGuid" dataField="LinkGuid"/>    //要显示的字段,按列显示
   <mx:DataGridColumn headerText="NodeGuid" dataField="NodeGuid"/>
   <mx:DataGridColumn headerText="ToWard" dataField="ToWard"/>
   <mx:DataGridColumn headerText="RoadName" dataField="RoadName"/>
  </mx:columns>
 </mx:DataGrid>