1 配置
SQL2005
Silverlight3.0
VS2008 sp1
2 数据库
建立数据库db_silverlight
建立表tb_student和tb_score
stu_id int primary key ,
stu_name varchar ( 10 ) not null ,
stu_number varchar ( 10 ) not null ,
stu_address varchar ( 50 ) not null );
create table tb_score(
stu_id int primary key ,
sco_math smallint not null ,
sco_english smallint not null ,
sco_chinese smallint not null ,
foreign key (stu_id) references tb_student(stu_id)
);
create view sil_view
as
select A.stu_id,A.stu_name,A.stu_number,A.stu_address,B.sco_math,B.sco_english,B.sco_chinese
from tb_student A,tb_score B
where A.stu_id = B.stu_id;
3 录入数据
insert into tb_score values ( 1 , 97 , 98 , 82 );
insert into tb_student values ( 2 , ' 陈守 ' , ' 02 ' , ' 甘肃省 ' );
insert into tb_score values ( 2 , 87 , 93 , 85 );
insert into tb_student values ( 3 , ' 卢大招 ' , ' 03 ' , ' 辽宁省 ' );
insert into tb_score values ( 3 , 97 , 92 , 81 );
insert into tb_student values ( 4 , ' 刘方 ' , ' 04 ' , ' 浙江省 ' );
insert into tb_score values ( 4 , 77 , 78 , 76 );
insert into tb_student values ( 5 , ' 周成 ' , ' 05 ' , ' 江苏省 ' );
insert into tb_score values ( 5 , 97 , 99 , 99 );
insert into tb_student values ( 6 , ' 庄小芳 ' , ' 06 ' , ' 黑龙江 ' );
insert into tb_score values ( 6 , 69 , 90 , 75 );
insert into tb_student values ( 7 , ' 张建国 ' , ' 07 ' , ' 广西省 ' );
insert into tb_score values ( 7 , 67 , 98 , 94 );
insert into tb_student values ( 8 , ' 艾青 ' , ' 08 ' , ' 江西省 ' );
insert into tb_score values ( 8 , 92 , 91 , 78 );
insert into tb_student values ( 9 , ' 郑成 ' , ' 09 ' , '河北省 ' );
insert into tb_score values ( 9 , 69 , 79 , 93 );
insert into tb_student values ( 10 , ' 萧条 ' , ' 10 ' , ' 西藏 ' );
insert into tb_score values ( 10 , 94 , 90 , 89 );
insert into tb_student values ( 11 , ' 钟涯 ' , ' 11 ' , ' 四川省 ' );
insert into tb_score values ( 11 , 97 , 88 , 82 );
insert into tb_student values ( 12 , ' 马云 ' , ' 12 ' , ' 陕西省 ' );
insert into tb_score values ( 12 , 96 , 98 , 70 );
insert into tb_student values ( 13 , ' 杨海亮 ' , ' 13 ' , ' 内蒙古 ' );
insert into tb_score values ( 13 , 93 , 98 , 80 );
insert into tb_student values ( 14 , ' 林森路 ' , ' 14 ' , ' 山东省 ' );
insert into tb_score values ( 14 , 97 , 93 , 60 );
insert into tb_student values ( 15 , ' 邱余云 ' , ' 15 ' , ' 山西省 ' );
insert into tb_score values ( 15 , 91 , 98 , 89 );
insert into tb_student values ( 16 , ' 苏小云 ' , ' 16 ' , ' 湖南省 ' );
insert into tb_score values ( 16 , 69 , 100 , 79 );
4 建立一个linq_to_sql_datagrid的silverlight应用程序
5 创建Linq to sql数据库模型类
将视图sil_view拖入到资源管理器中,利用视图进行数据显示
命名空间
6 建立WCF Service数据通信服务
不是而是
Service.svc.cs
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using sil_content; // 引用命名空间,使可以使用DataClasses1DataContext
using sil_entity; // 引用命名空间,使可以使用sil_view表
namespace linq_wfp_datagrid.Web
{
[ServiceContract(Namespace = "" )]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public void DoWork()
{
// 在此处添加操作实现
return ;
}
// 在此处添加更多操作并使用 [OperationContract] 标记它们
[OperationContract]
public List < sil_view > Get_Data()
{
DataClasses1DataContext dc = new DataClasses1DataContext();
var query = from q in dc.sil_view
select q;
return query.ToList < sil_view > ();
}
}
}
7 在linq_wfp_datagrid建立服务引用
8 MainPage.xaml
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d" d:DesignWidth ="640" d:DesignHeight ="480" >
< Grid x:Name ="LayoutRoot" >
< Grid.RowDefinitions >
< RowDefinition Height ="200" />
< RowDefinition Height ="35" />
</ Grid.RowDefinitions >
< data:DataGrid x:Name ="mydatagrid" Grid.Row ="0" AutoGenerateColumns ="False" ColumnWidth ="100" Width ="720" >
< data:DataGrid.Columns >
< data:DataGridTextColumn Header ="编号"
Binding =" {Binding stu_id} "
IsReadOnly ="True" />
< data:DataGridTextColumn Header ="姓名"
Binding =" {Binding stu_name} "
IsReadOnly ="True" />
< data:DataGridTextColumn Header ="学号"
Binding =" {Binding stu_number} "
IsReadOnly ="True" />
< data:DataGridTextColumn Header ="地址"
Binding =" {Binding stu_address} "
IsReadOnly ="True" />
< data:DataGridTextColumn Header ="数学"
Binding =" {Binding sco_math} "
IsReadOnly ="True" />
< data:DataGridTextColumn Header ="英语"
Binding =" {Binding sco_english} "
IsReadOnly ="True" />
< data:DataGridTextColumn Header ="语文"
Binding =" {Binding sco_chinese} "
IsReadOnly ="True" />
</ data:DataGrid.Columns >
</ data:DataGrid >
< Button x:Name ="button" Click ="button_Click" Grid.Row ="1" Content ="获取数据" Width ="100" ></ Button >
</ Grid >
</ UserControl >
9 MainPage.xaml.cs
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using linq_wfp_datagrid.ServiceReference1; // 添加服务引用
namespace linq_wfp_datagrid
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this .button.Click += new RoutedEventHandler(button_Click);
}
private void button_Click( object sender, RoutedEventArgs e)
{
Service1Client webclient = new Service1Client();
webclient.Get_DataCompleted += new EventHandler < Get_DataCompletedEventArgs > (webclient_Get_DataCompleted);
// 调用Get_Data的方法
webclient.Get_DataAsync();
}
private void webclient_Get_DataCompleted( object sender, Get_DataCompletedEventArgs e)
{
List < sil_view > stu = new List < sil_view > (e.Result);
mydatagrid.ItemsSource = stu;
}
}
}
10 运行效果:
11 总结
11.1 如果想调整DataGrid的列宽
DataGridColumn.Width 是 DataGridLength 类型,在 Silverlight 3.0 中只有:
absolutePixelValue
绝对列宽的正数值。
Auto
启用 Auto 调整大小的文本字符串值,可同时根据单元格和列标题的内容调整 DataGrid 列的大小。
SizeToCells
启用 SizeToCells 调整大小的文本字符串值,可根据列中单元格的内容(不包括列标题)调整 DataGrid 列的大小。
SizeToHeader
启用 SizeToHeader 调整大小的文本字符串值,仅根据列标题的内容调整 DataGrid 列的大小。
11.2 一个没有索引的视图其实只是一个虚拟表,没有任何物理的数据,它就是一段代码组成的东西,包含了描述它的元数据,如结构,依赖性等.
a. 它可以对基础数据做些聚合之类的操作后提供使用.
b.它可以筛选数据,让客户只能访问视图以达到保护原表的目的.
c.同样,视图不能包含单独的ORDER BY .因为它被认为是一个表,表是逻辑实体,本身里面的数据时随便放的,并米有刻意按照某个顺序存储数据.
故在一定程度上可以定性的进行Silverlight与SQL多表操作;
如果不是嵌套性读取,在效率方面影响是很小的,一般情况下视图会提高数据库的操作效率,也为软件的形成提供了一个很好的可视化数据表。
11.3 基本的实现方式
- 数据库实现
- Data SQL层实现(Linq)
- Web Service层实现
- Silverlight客户端实现
11.4 Silverlight与WCF、Linq、Database的关系
11.5 Linq的基本架构
LINQ是Language Integrated Query的简称,它是集成在.NET编程语言中的一种特性。已成为编程语言的一个组成部分,在编写程序时可以得到很好的编译时语法检查,丰富的元数据,智能感知、静态类型等强类型语言的好处。并且它同时还使得查询可以方便地对内存中的信息进行查询而不仅仅只是外部数据源。
LINQ定义了一组标准查询操作符用于在所有基于.NET平台的编程语言中更加直接地声明跨越、过滤和投射操作的统一方式,标准查询操作符允许查询作用于所有基于IEnumerable<T>接口的源,并且它还允许适合于目标域或技术的第三方特定域操作符来扩大标准查询操作符集,更重要的是,第三方操作符可以用它们自己的提供附加服务的实现来自由地替换标准查询操作符,根据LINQ模式的习俗,这些查询喜欢采用与标准查询操作符相同的语言集成和工具支持。
Linq的命名空间:
- LINQ包括五个部分:LINQ to Objects、LINQ to DataSets、LINQ to SQL、LINQ to Entities、LINQ to XML。
-
LINQ to XML在System.Xml.LINQ命名空间下实现对XML的操作。采用高效、易用、内存中的XML工具在宿主编程语言中提供 XPath/XQuery功能等。
11.6 相关参考资料
视图
http://blog.csdn.net/feixianxxx/archive/2009/11/11/4798287.aspx
Linq简介
http://www.cnblogs.com/lyj/archive/2008/01/20/1046196.html
Silverlight访问数据库之Linq to Sql篇