性能疯子 Blog

倚天照海花无数,流水山高心自知

LinQ技术--转

‘Microsof SQL Server’数据源的用于 SQL server .NET Framework 数据提供程序支持。
‘Microsof SQL Server’数据源的用于 OLE DB .NET Framework 数据提供程序不支持。
‘Microsoft ODBC 数据源用于 用于 ODBC .NET Framework 数据提供程序不支持。
‘Microsoft Access 数据库文件数据源的用于OLE DB.NET Framework 数据提供程序不支持。
即,对SQL Server的专用数据提供程序支持,对OLE DBODBC数据提供程序不支持,对Oralce专用属于提供程序未做测试

建议:

Linq to sql 的数据持久方式不能跨数据库平台,因此后台如果是sql server 数据库,可以使用此orm,调用过程最好是业务逻辑层’===数据访问层’====Linq to sql orm对象,这样,如果要跨数据库,则需要重写数据访问层(此数据访问层相当于一个代理),对上面的各层不会产生影响。

  1  protected void Page_Load(object sender, EventArgs e)
2 {
3 DataPersonDataContext dc = new DataPersonDataContext();
4 //查询2
5 GridView1.DataSource = from p in dc.Person
6 where p.ID < 100 && p.Name.StartsWith("bei")
7 select new { 编码 = p.ID, 姓名 = p.Name, 电话 = p.Tel };
8 //查询3
9 var varPerson = from p in dc.Person
10 where p.ID < 100 && p.Tel.StartsWith("010")
11 orderby p.Name descending
12 select new { 编码 = p.ID, 姓名 = p.Name, 电话 = p.Tel };
13 GridView1.DataSource = varPerson;
14 //添加
15 Person person1 = new Person();
16 person1.Name = "xianggang";
17 person1.Tel = "00852-11111111";
18 dc.Person.InsertOnSubmit(person1);
19 dc.SubmitChanges();
20 //更新
21 Person person2 = dc.Person.Where(p => p.Name == "xianggang").First();
22 person2.Tel = "00852-11111110";
23 dc.SubmitChanges();
24
25 //删除
26 Person person3 = dc.Person.Where(p => p.Name == "xianggang").First();
27 dc.Person.DeleteOnSubmit(person3);
28 dc.SubmitChanges();
29 //查询1
30 GridView1.DataSource = dc.Person;
31 GridView1.DataBind();
32 }
33 =>
34 返回结果:
35 ID Name Tel
36 1 beijing 010-0000000
37 2 shanghai 021-0000000
38 13 xianggang 00852-11111111
39 14 xianggang 00852-11111111
40 15 xianggang 00852-11111111
41 16 xianggang 00852-11111111
42 =>
43 =>
44 1、创建linq to sql(即DataPerson.dbml文件):
45 #pragma warning disable 1591
46 //------------------------------------------------------------------------------
47 // <auto-generated>
48 // 此代码由工具生成。
49 // 运行库版本:2.0.50727.1433
50 //
51 // 对此文件的更改可能会导致不正确的行为,并且如果
52 // 重新生成代码,这些更改将会丢失。
53 // </auto-generated>
54 //------------------------------------------------------------------------------
55 using System;
56 using System.Collections.Generic;
57 using System.ComponentModel;
58 using System.Data;
59 using System.Data.Linq;
60 using System.Data.Linq.Mapping;
61 using System.Linq;
62 using System.Linq.Expressions;
63 using System.Reflection;
64 [System.Data.Linq.Mapping.DatabaseAttribute(Name="msdb")]
65 public partial class DataPersonDataContext : System.Data.Linq.DataContext
66 {
67
68 private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
69
70 #region Extensibility Method Definitions
71 partial void OnCreated();
72 partial void InsertPerson(Person instance);
73 partial void UpdatePerson(Person instance);
74 partial void DeletePerson(Person instance);
75 #endregion
76
77 public DataPersonDataContext() :
78 base(global::System.Configuration.ConfigurationManager.ConnectionStrings["msdbConnectionString"].ConnectionString, mappingSource)
79 {
80 OnCreated();
81 }
82
83 public DataPersonDataContext(string connection) :
84 base(connection, mappingSource)
85 {
86 OnCreated();
87 }
88
89 public DataPersonDataContext(System.Data.IDbConnection connection) :
90 base(connection, mappingSource)
91 {
92 OnCreated();
93 }
94
95 public DataPersonDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
96 base(connection, mappingSource)
97 {
98 OnCreated();
99 }
100
101 public DataPersonDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
102 base(connection, mappingSource)
103 {
104 OnCreated();
105 }
106
107 public System.Data.Linq.Table<Person> Person
108 {
109 get
110 {
111 return this.GetTable<Person>();
112 }
113 }
114 }
115 [Table(Name="dbo.Person")]
116 public partial class Person : INotifyPropertyChanging, INotifyPropertyChanged
117 {
118
119 private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
120
121 private int _ID;
122
123 private string _Name;
124
125 private string _Tel;
126
127 #region Extensibility Method Definitions
128 partial void OnLoaded();
129 partial void OnValidate(System.Data.Linq.ChangeAction action);
130 partial void OnCreated();
131 partial void OnIDChanging(int value);
132 partial void OnIDChanged();
133 partial void OnNameChanging(string value);
134 partial void OnNameChanged();
135 partial void OnTelChanging(string value);
136 partial void OnTelChanged();
137 #endregion
138
139 public Person()
140 {
141 OnCreated();
142 }
143
144 [Column(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
145 public int ID
146 {
147 get
148 {
149 return this._ID;
150 }
151 set
152 {
153 if ((this._ID != value))
154 {
155 this.OnIDChanging(value);
156 this.SendPropertyChanging();
157 this._ID = value;
158 this.SendPropertyChanged("ID");
159 this.OnIDChanged();
160 }
161 }
162 }
163
164 [Column(Storage="_Name", DbType="NChar(10)")]
165 public string Name
166 {
167 get
168 {
169 return this._Name;
170 }
171 set
172 {
173 if ((this._Name != value))
174 {
175 this.OnNameChanging(value);
176 this.SendPropertyChanging();
177 this._Name = value;
178 this.SendPropertyChanged("Name");
179 this.OnNameChanged();
180 }
181 }
182 }
183
184 [Column(Storage="_Tel", DbType="NChar(20)")]
185 public string Tel
186 {
187 get
188 {
189 return this._Tel;
190 }
191 set
192 {
193 if ((this._Tel != value))
194 {
195 this.OnTelChanging(value);
196 this.SendPropertyChanging();
197 this._Tel = value;
198 this.SendPropertyChanged("Tel");
199 this.OnTelChanged();
200 }
201 }
202 }
203
204 public event PropertyChangingEventHandler PropertyChanging;
205
206 public event PropertyChangedEventHandler PropertyChanged;
207
208 protected virtual void SendPropertyChanging()
209 {
210 if ((this.PropertyChanging != null))
211 {
212 this.PropertyChanging(this, emptyChangingEventArgs);
213 }
214 }
215
216 protected virtual void SendPropertyChanged(String propertyName)
217 {
218 if ((this.PropertyChanged != null))
219 {
220 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
221 }
222 }
223 }
224 #pragma warning restore 1591
225 3.linq多表查询
226 sql语句
227 select t_friend.userid,friendid,truename,formid
228 from dbo.t_friend left join dbo.t_userinfo
229 on dbo.t_friend.friendid=dbo.t_userinfo.userid
230 where dbo.t_friend.userid=5 and applystate=1
231 linq语句
232 public void bind()
233 {
234 blogdatabaseDataContext dc = new blogdatabaseDataContext();
235 Table<t_userinfo> login = dc.GetTable<t_userinfo>();
236 Table<t_friend> t_f=dc.GetTable<t_friend>();
237 var info = from t in t_f
238 join c in login on t.friendid equals c.userid
239 where( t.userid == 5 && t.applystate == true)
240 select new
241 {
242 t.formid,
243 t.userid,
244 t.friendid,
245 c.truename,
246 c.picture
247 };
248 this.GridView1.DataSource = info;
249 this.GridView1.DataBind();
250 }
posted @ 2011-07-22 13:20  性能疯子  阅读(411)  评论(0编辑  收藏  举报
QQ联系我