您的位置:首页 > 编程语言 > C#

Flex中调用C#.NET里的WebService方法

2009-03-30 21:06 483 查看
首先在VS里新建一个WebService,默认情况下它已经给出了一个HelloWorld方法。



【C#中的代码如下】:

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Data;

using System.Data.SqlClient;

[WebService(Namespace = http://tempuri.org/)] //默认情况下的命名空间,只用于开发阶段,实际发布是最好更改

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

public Service () { } //默认生成的一个HelloWorld()方法

[WebMethod]

public string HelloWorld() {

return "Hello World";

}

[WebMethod]

public string getConnection(string sql) //自定义一个WebService方法,模拟登陆

{

SqlConnection conn = new SqlConnection("Data Source=.; Initial Catalog=HappyHome; uid=sa; pwd=sa");

SqlDataAdapter da = new SqlDataAdapter(sql, conn);

DataSet ds = new DataSet();

da.Fill(ds);

return ds.GetXml();

}

}





【Flex中的代码如下】:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>

<![CDATA[

import mx.rpc.events.ResultEvent;

import mx.controls.Alert;

[Bindable]public var getresultXML:XML=new XML(); //定义一个全局变量用来接收WebService返回的XML数据

public var islogin:Boolean=new Boolean(false); //定义一个boolean类型的变量来判断是否登陆成功!

private function btnReset_Click():void //“重置”按钮点击事件,清空文本框 {

this.txtUserName.text="";

this.txtUserPwd.text="";

}

private function btnLogin_Click():void //"登陆"按钮点击事件

{

if(this.txtUserName.text=="" || this.txtUserPwd.text=="")

{

Alert.show("用户名和密码不能为空!");

return;

}

else

{

//把文本框里输入的信息封装到SQL语句中,并作为参数传到WebService方法里。

var strLoginSQL:String="select * from Users where Email='"+this.txtUserName.text+"' and LoginPwd='"+this.txtUserPwd.text+"'";

//调用WebService里自定义的方法getConnection(string sql)方法

this.WebServiceTest.getConnection(strLoginSQL);

}

}

private function onResult(event:ResultEvent):void

{

getresultXML=XML(event.result);

if(getresultXML.toString().length>0)

{

var strNickName:String=XML(event.result.toString()).Table.NickName.toString();

this.islogin=true;

Alert.show("登陆成功!欢迎您"+strNickName);

}

else

{

Alert.show("没有查到此用户!登陆失败!");

return;

}

}

]]>

</mx:Script>

//定义一个WebService组件,

//自定义一个id,以便在方法中引用。

//Wsdl是WebService的地址,可以直接在VS里运行WebService,在浏览器里的地址就是了。

//useProxy 是否使用代理服务器

// fault 当出错时触发的方法

// result 当成功时监听的方法

<mx:WebService id="WebServiceTest"

wsdl="http://localhost:1427/TestWebService/Service.asmx?WSDL"

useProxy="false"

fault="Alert.show(event.fault.faultString),'出错了'"

result="onResult(event)">

<mx:operation name="HelloWorld"/> // 可以把默认的HelloWorld引入,只是作为测试

<mx:operation name="getConnection"/> //引入真正要调用的方法

</mx:WebService>

<mx:Panel x="201.5" y="104" width="293" height="189" layout="absolute" id="loginPanal" title="登陆面板" status="Design By Eule">

<mx:Label x="18" y="24" text="邮件地址:" fontSize="12"/>

<mx:Label x="18" y="50" text="登陆密码:" fontSize="12"/>

<mx:TextInput x="91" y="24" id="txtUserName"/>

<mx:TextInput x="91" y="50" id="txtUserPwd"/>

<mx:Button x="91" y="80" label="登陆" id="btnLogin" click="btnLogin_Click()"/>

<mx:Button x="147" y="80" label="重置" id="btnReset" click="btnReset_Click()"/>

</mx:Panel>

</mx:Application>





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: