热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

silverlight通过WCF访问SQLserver示例

VS2013+sql2012+win8.1测试通过作者:一剑1.创建sliverlight项目testWCFsql;2.右击web项目添加新建项-类,命名为ClassDBserver,修改代码:usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Web.Configuration;namespacetestWCFsql.Web{

VS2013+sql2012+win8.1测试通过 作者:一剑 1.创建sliverlight项目testWCFsql; 2.右击web项目添加新建项-类,命名为ClassDBserver,修改代码: using System.Data; using System.Data.SqlClient; using System.Web.Configuration; namespace testWCFsql.Web{

VS2013+sql2012+win8.1测试通过 作者:一剑

1.创建sliverlight项目testWCFsql;
2.右击web项目添加新建项->类,命名为ClassDBserver,修改代码:

using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace testWCFsql.Web
{
    public class ClassDBserver
    {
        static public DataSet DBselect(string sql)
        {
            string conn;
            conn = "Data Source=none;Initial Catalog=comDB;User ID=sa;Password=123456";
            //cOnn= WebConfigurationManager.AppSettings.Get("DBappSettings");
            //cOnn= WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            try
            {
                using (SqlConnection cnn = new SqlConnection(conn))
                {
                    cnn.Open();
                    using (SqlTransaction trans = cnn.BeginTransaction(IsolationLevel.Serializable))
                    {
                        try
                        {
                            using (SqlCommand cmd = new SqlCommand(sql, cnn))
                            {
                                cmd.Transaction = trans;
                                using (DataSet ds = new DataSet())
                                {
                                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                                    {
                                        da.Fill(ds);
                                        trans.Commit();
                                    }
                                    return ds;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            return new DataSet();
                            throw ex;
                        }
                        finally
                        {
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return new DataSet();
                throw ex;
            }
        }
    }
}

3.右击testWCFsql.web项目添加新建项“启用了Silverlight的WCF服务”,命名为ServiceWCF;
4.修改

[OperationContract]
public void DoWork()
{
  return;
 }

using System.Data;
using System.Collections.Generic;

[OperationContract]
public List getData()
{
    DataSet ds = ClassDBserver.DBselect("select * from employee");
    List userList = new List();//添加using System.Collections.Generic;
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        ClassEmployee user = new ClassEmployee();//userList是引用类型,所以这句不能放foreach外面
        user.ID = (int)dr["ID"];
        user.name = (string)dr["name"];
        user.departmentID = (int)dr["departmentID"];
        userList.Add(user);
    }
    return userList;
}
[DataContract]
public class ClassEmployee
{
    [DataMember]
    public int ID;
    [DataMember]
    public string name;
    [DataMember]
    public int departmentID;
}

5.生成一下;
6.右击客户端testWCFsql项目,添加服务引用->发现,默认ServiceReference1,确定;
7.双击页面文件MainPage.xaml.cs,从工具箱中拖入DataGrid控件,命名为datagrid1;
8.修改后台代码为:

using System.Xml;
using System.IO;

namespace testWCFsql
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            ServiceReference1.ServiceWCFClient myClient = new ServiceReference1.ServiceWCFClient();
            myClient.getDataCompleted += new EventHandler(myGetDataCompleted);
            myClient.getDataAsync();
        }

        void myGetDataCompleted(object sender, ServiceReference1.getDataCompletedEventArgs e)
        {
            System.Collections.ObjectModel.ObservableCollection users = e.Result;
            datagrid1.ItemsSource = users;
        }
    }
}

9.此步可选:双击Weg.config修改,在


    

之间插入

    
      "DBappSetting" value="Data Source=none;Initial Catalog=comDB;User ID=sa;Password=123456"/>
    
    
      "myConnectionString" cOnnectionString="Data Source=none;Initial Catalog=comDB;User ID=sa;Password=123456" providerName="System.Data.SqlClient"/>
    

同时修改前面ClassDBserver.cs中的连接字符串为从配置文件中读取。
10.Now,just run it!!

推荐阅读
author-avatar
天飞的鹊桥会大美女
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有