亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

通過反射生成SQL的例子

系統 2286 0

  全文摘自http://www.cnblogs.com/g1mist/p/3227290.html,很好的一個實例。

  反射提供了封裝程序集、模塊和類型的對象。您可以使用反射動態地創建類型的實例,將類型綁定到現有對象,或從現有對象中獲取類型。然后,可以調用類型的方法或訪問其字段和屬性。

  1.先建立實體類

  用戶實體類:

1
2
3
4
5
6
7
8
9
public class User
???? {
???????? public int id { get ; set ; }
???????? public string UserName { get ; set ; }
???????? public string Password { get ; set ; }
???????? public int Age { get ; set ; }
???????? public string PhoneNumber { get ; set ; }
???????? public string Address { get ; set ; }
???? }

  書籍實體類:

1
2
3
4
5
6
7
8
public class Book
??? {
??????? public int id { set ; get ; }
??????? public string BookName { get ; set ; }
??????? public string ISBN { set ; get ; }
??????? public string Author { set ; get ; }
??????? public double Price { set ; get ; }
??? }

  2.通過反射技術來生成Insert語句(舉個例子而已,只生成Insert語句)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
?? /// <summary>
?? /// 泛型方法,反射生成SQLInsert語句
/// </summary>
?? /// <typeparam name="T">實體類型</typeparam>
?? /// <param name="entity">實體對象</param>
?? /// <returns></returns>
?? public string CreateInsertSQL<T>(T entity)
?? {
?????? //1.先獲取實體的類型描述
?????? Type type = entity.GetType();
?????? //2.獲得實體的屬性集合
?????? PropertyInfo[] props = type.GetProperties();
?
?????? //實例化一個StringBuilder做字符串的拼接
?? StringBuilder sb = new StringBuilder();
?
?????? sb.Append( "insert into " + type.Name + " (" );
?
?????? //3.遍歷實體的屬性集合
?????? foreach (PropertyInfo prop in props)
?????? {
?????????? //4.將屬性的名字加入到字符串中
????? sb.Append(prop.Name + "," );
?????? }
?????? //**去掉最后一個逗號
??? sb.Remove(sb.Length - 1, 1);
?????? sb.Append( " ) values(" );
?
?????? //5.再次遍歷,形成參數列表"(@xx,@xx@xx)"的形式
?????? foreach (PropertyInfo prop in props)
?????? {
?????????? sb.Append( "@" + prop.Name + "," );
?????? }
?????? //**去掉最后一個逗號
?????? sb.Remove(sb.Length - 1, 1);
?????? sb.Append( ")" );
?
?????? return sb.ToString();
?? }

  3.測試

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Program
???? {
???????? static void Main( string [] args)
???????? {
//調用ReflationCreateSQL類中的CreateInsertSQL方法??????????
???????? string sql = new ReflationCreateSQL().CreateInsertSQL( new User());
???????????? string sql1 = new ReflationCreateSQL().CreateInsertSQL( new Book());
?
???????????? Console.WriteLine(sql.ToString());
???????????? Console.WriteLine(sql1.ToString());
?
???????????? Console.WriteLine( "Press any key to continue . . ." );
???????????? Console.ReadLine();
???????? }
???? }

  結果:

  但是,我們發現id是主鍵,假設id是自增長的,我們生成的SQL(Insert)語句中就不應該有id,在這里我用自定義Attribute的方法來解決這個問題。

  4.先新建一個類,繼承Attribute類

1
2
3
4
public class KEYAttribute : Attribute
{
?
}

  這個類僅此而已就夠了。

  5.在實體類中的id這個字段上加上[KEY]標記

1
2
3
4
5
6
7
8
9
public class Book
???? {
???????? [KEY]
???????? public int id { set ; get ; }
???????? public string BookName { get ; set ; }
???????? public string ISBN { set ; get ; }
???????? public string Author { set ; get ; }
???????? public double Price { set ; get ; }
???? }

?

1
2
3
4
5
6
7
8
9
10
public class User
??? {
??????? [KEY]
??????? public int id { get ; set ; }
??????? public string UserName { get ; set ; }
??????? public string Password { get ; set ; }
??????? public int Age { get ; set ; }
??????? public string PhoneNumber { get ; set ; }
??????? public string Address { get ; set ; }
??? }

  6.加好標記之后,我們只需要這CreateInsertSQL<T>(T entity)這個方法中的兩個foreach循環體中加一些判斷即可

1
2
3
4
5
6
7
8
9
10
11
12
foreach (PropertyInfo prop in props)
??????????? {
??????????????? //獲取用戶自定義標記集合
?????????? object [] attrs = prop.GetCustomAttributes( typeof (KEYAttribute), true );
??????????????? //如果屬性上有自定義標記KEYAttribute,退出本次循環
??????????? if (attrs.Length > 0)
??????????????? {
??????????????????? continue ;
??????????????? }
??????????????? //將屬性的名字加入到字符串中
?????????? sb.Append(prop.Name + "," );
??????????? }

1
2
3
4
5
6
7
8
9
foreach (PropertyInfo prop in props)
???????????? {
???????????????? object [] attrs = prop.GetCustomAttributes( typeof (KEYAttribute), true );
???????????????? if (attrs.Length > 0)
???????????????? {
???????????????????? continue ;
???????????????? }
???????????????? sb.Append( "@" + prop.Name + "," );
???????????? }

  7.測試

通過反射生成SQL的例子

通過反射生成SQL的例子


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 91福利一区二区在线观看 | 国产免费久久精品久久久 | 一级网站在线观看 | 午夜精品久久久久久久第一页 | 日本一级大毛片a一 | 麻豆精品 | 特级毛片 | 亚洲精品欧洲久久婷婷99 | 国产aaa免费视频国产 | 一区二区中文字幕 | 一级a性色生活片毛片 | 欧美日韩视频精品一区二区 | 久久精品国产大片免费观看 | 亚洲成人18 | 欧美亚洲国产一区二区三区 | 久久综合九色综合97婷婷女人 | 免费播放欧美毛片欧美aaaaa | 精品国产人成在线 | 久久艹在线观看 | 五月天丁香婷婷综合久久 | 亚洲一区欧美日韩 | 亚洲综色 | 在线免费观看a视频 | 国产精品亚洲欧美日韩久久 | 秋霞伊人 | 欧美毛片网站 | 日韩毛片一级 | 国产综合久久一区二区三区 | 国产高清久久99 | 欧美经典人人爽人人爽人人片 | 一区二区三区在线 | 网站 | 国产成人福利夜色影视 | 麻豆精品永久免费视频 | 免费一级网站 | 天天草夜夜爽 | 91手机在线视频 | 91视频最新网址 | 日本一级毛片免费看 | 国产成人精品男人的天堂538 | 性欧美暴力猛交xxxxx高清 | 国产日韩在线看 |