Silverlight调用WCF(1)

Silverlight调用WCF(1),第1张

概述代码下载 程序结构 移动手机开发企业应用,常常会访问远程数据库(云端数据库),往往通过WCF对外提供接口访问。程序结构一般是:Silverlight+WCF+Sql Server数据库 下面就是以 *** 作用户User为例,移动终端通过调用WCF实现对数据库的基本 *** 作(增删改查)。 1.数据库 1)创建数据库表 CREATE TABLE [dbo].[User](  [UserID] [int] IDE

代码下载

程序结构

移动手机开发企业应用,常常会访问远程数据库(云端数据库),往往通过WCF对外提供接口访问。程序结构一般是:Silverlight+WCF+sql Server数据库

下面就是以 *** 作用户User为例,移动终端通过调用WCF实现对数据库的基本 *** 作(增删改查)。
1.数据库
1)创建数据库表
CREATE table [dbo].[User](
 [UserID] [int] IDENTITY(1,1) NOT NulL,
 [Username] [nchar](100) NOT NulL,
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
 [UserID] ASC
)WITH (PAD_INDEX  = OFF,STATISTICS_norECOmpuTE  = OFF,IGnorE_DUP_KEY = OFF,ALLOW_ROW_LOCKS  = ON,ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
2.WCF
1)定义接口
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string RetrIEveUser();
        [OperationContract]
        bool createuser(string username);
        [OperationContract]
        bool UpdateUser(int userID,string username);
        [OperationContract]
        bool DeleteUser(int userID);
    }
2)实现接口
 public class Service1 : IService1
    {
        //查询用户
        public string RetrIEveUser()
        {
            try
            {
                sqlConnection _sqlConnection =
                   new sqlConnection("Database=test1;Server=lochost;Integrated Security=false;password=1;user ID=sa;");
                _sqlConnection.open();
                sqlDataAdapter da = new sqlDataAdapter();
                da.SelectCommand = new sqlCommand("SELECT * FROM [User]",
                     _sqlConnection);
                DataSet ds = new DataSet();
                da.Fill(ds);
                StringBuilder sb = new StringBuilder();
                sb.Append("<?xml version=\"1.0\" enCoding=\"utf-8\" ?>");
                sb.Append("<Users>");
                foreach (DaTarow dr in ds.tables[0].Rows)
                {
                    sb.Append("<User>");
                    sb.Append("<UserID>");
                    sb.Append(dr[0].ToString());
                    sb.Append("</UserID>");
                    sb.Append("<Username>");
                    sb.Append(dr[1].ToString());
                    sb.Append("</Username>");
                    sb.Append("</User>");
                }
                sb.Append("</Users>");
                _sqlConnection.Close();
                return sb.ToString();
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
        //创建用户
        public bool createuser(string username)
        {
            try
            {
                sqlConnection _sqlConnection =
                    new sqlConnection("Database=test1;Server=lochost;Integrated Security=false;password=1;user ID=sa;");
                _sqlConnection.open();
                sqlCommand command = new sqlCommand();
                command.Connection = _sqlConnection;
                command.CommandType = CommandType.Text;
                command.CommandText = "INSERT INTO [User]  ([Username]) VALUES ('" +
                    username.ToString().Replace("'","''") + "')";
                command.ExecuteNonquery();
                _sqlConnection.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        //更新用户
        public bool UpdateUser(int userID,string username)
        {
            try
            {
                sqlConnection _sqlConnection =
                    new sqlConnection("Database=test1;Server=lochost;Integrated Security=false;password=1;user ID=sa;");
                _sqlConnection.open();
                sqlCommand command = new sqlCommand();
                command.Connection = _sqlConnection;
                command.CommandType = CommandType.Text;
                command.CommandText = "UPDATE [User] " +
                    "SET [Username] = '" +
                     username.ToString().Replace("'","''") + "'" +
                    "WHERE [UserID] = " + userID.ToString();
                command.ExecuteNonquery();
                _sqlConnection.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        //删除用户
        public bool DeleteUser(int userID)
        {
            try
            {
                sqlConnection _sqlConnection =
                    new sqlConnection("Database=test1;Server=lochost;Integrated Security=false;password=1;user ID=sa;");
                _sqlConnection.open();
                sqlCommand command = new sqlCommand();
                command.Connection = _sqlConnection;
                command.CommandType = CommandType.Text;
                command.CommandText = "DELETE [User] WHERE [UserID] = "
                                      + userID.ToString();
                command.ExecuteNonquery();
                _sqlConnection.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
3)允许跨域访问
建立clIEntaccesspolicy.xml文件,放于WCF项目根目录
<?xml version="1.0" enCoding="utf-8" ?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>
3.Silverlight
1)添加Web引用
2)异步调用WCF中对象方法
 private ServiceReference1.Service1ClIEnt userSvcclIEnt;
        private voID button1_Click(object sender,RoutedEventArgs e)
        {
            userSvcclIEnt = new ServiceReference1.Service1ClIEnt();
            //模拟一个用户
            string username = "zhaoyu";
            //注册createuserCompleted事件
            userSvcclIEnt.createuserCompleted += new EventHandler<ServiceReference1.createuserCompletedEventArgs>(userSvcclIEnt_createuserCompleted);
            //调用createuserAsync()方法创建用户
            userSvcclIEnt.createuserAsync(username);
        }

        voID userSvcclIEnt_createuserCompleted(object sender,ServiceReference1.createuserCompletedEventArgs e)
        {
            //完成createuserAsync()方法后回调.
            if (e.Error == null)
            {
                errMessage.Content = "创建用户成功!";

            }
            else
            {
                errMessage.Content = e.Error.ToString();

            }
        }

 

代码下载

总结

以上是内存溢出为你收集整理的Silverlight调用WCF(1)全部内容,希望文章能够帮你解决Silverlight调用WCF(1)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1068778.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存