欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

silverlight通过WCF访问SQLserver示例

程序员文章站 2022-06-06 20:28:50
...

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修改,在

之间插入

    "