房租管理小软件(四):对linq的使用

1.对LInq的封装如下:

 1    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
 2         public static MyFZDataContext a = null;
 3         static String path = Application.StartupPath;
 4         public MyFZDataContext(): base(getConnstring(), mappingSource)
 5         {
 6 
 7         }
 8 
 9         public MyFZDataContext(bool b): base(getConnstring(), mappingSource)
10         {
11             this.ObjectTrackingEnabled = b;
12         }
13 
14         
15         public static string connstring = "";
16         public static XmlSerialize xml = new XmlSerialize();
17         public static Common.Setting set = xml.DeSerialize();
18         
19         public static string getConnstring()
20         {
21             String Ip = set.IP;
22             string user = set.User;
23             string pass = set.Password;
24             string DB = set.DB;
25             connstring = "Data Source=" + Ip + ";Initial Catalog=" + DB + ";Persist Security Info=True;User ID=" + user + ";Password=" + pass + "" + ";Application Name=" + "NetHotel";
26             return connstring;
27         }
28         
29         public static MyFZDataContext getDataContext()
30         {
31             if (a == null)
32             {
33 
34                 a = new MyFZDataContext(false);
35             }
36             return a;
37 
38         }
View Code

 2.在代码中使用时加入

1 using System.Data.Linq;
2 using System.Linq;
3 using DataBase;
View Code

3.一个从数据库,得到数据的例子

1 MyFZDataContext dataContext = new MyFZDataContext();
2             var v1 = from t in dataContext.VIEW_LD 
3                      where
4                      t.账号.Contains(textEdit_Search.Text) ||
5                      t.手机.Contains(textEdit_Search.Text) ||
6                      t.承租人.Contains(textEdit_Search.Text)
7                      select t;
8             DataTable dt = DataFunction.LINQToDataTable(v1);
9             this.gridControl1.DataSource = dt;
View Code

4.事务的使用

 1 #region 增加//增加
 2                 MyFZDataContext dataContext = new MyFZDataContext();
 3                 if (dataContext.Connection != null) { dataContext.Connection.Open(); }
 4                 System.Data.Common.DbTransaction tran = dataContext.Connection.BeginTransaction();
 5                 dataContext.Transaction = tran;
 6                 try
 7                 {
 8                                       dataContext.SubmitChanges();
 9 
10                     tran.Commit();
11                     MessageBox.Show("保存成功");
12                     this.Close();
13                 }
14                 catch (Exception ex)
15                 {
16                     
17                     tran.Rollback();
18                     MessageBox.Show(ex.Message);
19                 }
20                 #endregion 
View Code

 5.把得到数据转成dataTable

 1 public static DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
 2         {
 3             DataTable dtReturn = new DataTable();
 4 
 5             // column names 
 6             PropertyInfo[] oProps = null;
 7 
 8             if (varlist == null) return dtReturn;
 9 
10             foreach (T rec in varlist)
11             {
12                 //// Use reflection to get property names, to create table, Only first time, others 
13                 //will follow 
14                 if (oProps == null)
15                 {
16                     oProps = ((Type)rec.GetType()).GetProperties();
17                     foreach (PropertyInfo pi in oProps)
18                     {
19                         Type colType = pi.PropertyType;
20 
21                         if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
22                         == typeof(Nullable<>)))
23                         {
24                             colType = colType.GetGenericArguments()[0];
25                         }
26 
27                         dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
28                     }
29                 }
30 
31                 DataRow dr = dtReturn.NewRow();
32 
33                 foreach (PropertyInfo pi in oProps)
34                 {
35                     dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
36                     (rec, null);
37                 }
38 
39                 dtReturn.Rows.Add(dr);
40             }
41             return dtReturn;
42         }
View Code

 

 

 

posted @ 2013-07-14 12:26  xiajing12345  阅读(302)  评论(0编辑  收藏  举报