日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁(yè)西西教程數(shù)據(jù)庫(kù)教程 → LinqToSql中Dbml文件提取建表TSql-CodeSmith

      LinqToSql中Dbml文件提取建表TSql-CodeSmith

      相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:本站整理時(shí)間:2010/9/27 14:29:55字體大。A-A+

      作者:佚名點(diǎn)擊:347次評(píng)論:0次標(biāo)簽: Sql

      • 類(lèi)型:電子教程大。8.5M語(yǔ)言:中文 評(píng)分:8.3
      • 標(biāo)簽:
      立即下載

      昨天一個(gè)大學(xué)師弟,他問(wèn)我能不能將LinqToSql文件轉(zhuǎn)化為創(chuàng)建表的TSql語(yǔ)句,他是剛開(kāi)始學(xué)習(xí).NET,所以在網(wǎng)上下些示例看,但苦于沒(méi)有數(shù)據(jù)庫(kù)。所以就有了這一篇博客,作為我的Code生成技術(shù)的CodeSimth的最后一篇示例。在下一步Code 生成技術(shù)將轉(zhuǎn)到Microsoft的T4模板,Code生成技術(shù)目前完成的有CodeDom,CodeSmith模板,高手請(qǐng)不要拍磚,請(qǐng)直接跳過(guò)。
      在Linq2Sql的Dbml文件其實(shí)就是一個(gè)Xml文件,記錄著數(shù)據(jù)庫(kù)與生成Linq2SqlCode的數(shù)據(jù)信息,所以轉(zhuǎn)化為T(mén)Sql沒(méi)有什么說(shuō)的。我們需要提取其中的數(shù)據(jù)庫(kù)信息,在轉(zhuǎn)化為我們的Tsql,在這里建立了DBTable、DBColumn、DBAssociation三個(gè)實(shí)體類(lèi):

      代碼
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5
      6 namespace DbmlToTable
      7 {
      8 public class DBTable
      9 {
      10
      11 public DBTable()
      12 {
      13 Columns = new List();
      14 this.Associations = new List();
      15 }
      16
      17 public string TableName
      18 {
      19 get;
      20 set;
      21 }
      22
      23 public List Columns
      24 {
      25 get;
      26 set;
      27 }
      28
      29 public List Associations
      30 {
      31 get;
      32 set;
      33 }
      34
      35 }
      36
      37 public class DBColumn
      38 {
      39 public string Name
      40 {
      41 get;
      42 set;
      43 }
      44
      45 public string DBType
      46 {
      47 get;
      48 set;
      49 }
      50
      51 public bool IsPrimaryKey
      52 {
      53 get;
      54 set;
      55 }
      56
      57 public bool IsDbGenerated
      58 {
      59 get;
      60 set;
      61 }
      62
      63 public bool CanBeNull
      64 {
      65 get;
      66 set;
      67 }
      68 }
      69
      70 public class DBAssociation
      71 {
      72 public string Name
      73 {
      74 get;
      75 set;
      76 }
      77
      78 public string ThisKey
      79 {
      80 get;
      81 set;
      82 }
      83
      84 public string OtherKey
      85 {
      86 get;
      87 set;
      88 }
      89
      90 public bool IsForeignKey
      91 {
      92 get;
      93 set;
      94 }
      95 }
      96
      97 public class DBTableHlper
      98 {
      99 public static DBTable GetAssociationTable(List collection,string assName)
      100 {
      101
      102 return collection.Find(t => t.Associations.Find(a => !a.IsForeignKey && a.Name == assName) != null);
      103 }
      104 }
      105 }
      106
      107
      其中DBTableHlper是由于我的Codesimth是2.0版本的,不能用lamdam表達(dá)式,所以我將它編譯在程序集里面。

      建立了一個(gè) 將我們的dbml文件xml Document轉(zhuǎn)化為實(shí)體類(lèi)輔助類(lèi):

      代碼
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Xml;
      6 using System.Xml.Linq;
      7
      8 namespace DbmlToTable
      9 {
      10
      11 public interface IDbTableCollectionHelper
      12 {
      13 List Transport(XElement element);
      14 }
      15
      16 public class DbTableCollectionHelper : IDbTableCollectionHelper
      17 {
      18 #region IDbTableCollectionHelper 成員
      19
      20 public List Transport(XElement element)
      21 {
      22 List collection = new List();
      23 var tables = element.Elements(XName.Get("Table", "http://schemas.microsoft.com/linqtosql/dbml/2007%22));
      24 foreach (var tab in tables)
      25 {
      26 DBTable t = new DBTable() { TableName = tab.Attribute("Name").Value };
      27 var cols = tab.Element(XName.Get("Type", "http://schemas.microsoft.com/linqtosql/dbml/2007%22)).Elements(XName.Get(%22Column%22, "http://schemas.microsoft.com/linqtosql/dbml/2007%22));
      28 foreach (var col in cols)
      29 {
      30 DBColumn c = new DBColumn()
      31 {
      32 CanBeNull = col.Attribute("CanBeNull") != null ? col.Attribute("CanBeNull").Value.ToLower() == "true" : false,
      33 DBType = col.Attribute("DbType") != null ? col.Attribute("DbType").Value : "",
      34 IsDbGenerated = col.Attribute("IsDbGenerated") != null ? col.Attribute("IsDbGenerated").Value.ToLower() == "true" : false,
      35 IsPrimaryKey = col.Attribute("IsPrimaryKey") != null ? col.Attribute("IsPrimaryKey").Value.ToLower() == "true" : false,
      36 Name = col.Attribute("Name") != null ? col.Attribute("Name").Value : ""
      37 };
      38 t.Columns.Add(c);
      39 }
      40
      41 var ass = tab.Element(XName.Get("Type", "http://schemas.microsoft.com/linqtosql/dbml/2007%22)).Elements(XName.Get(%22Association%22, "http://schemas.microsoft.com/linqtosql/dbml/2007%22));
      42 foreach (var item in ass)
      43 {
      44 DBAssociation a = new DBAssociation()
      45 {
      46 Name = item.Attribute("Name") != null ? item.Attribute("Name").Value : "",
      47 OtherKey = item.Attribute("OtherKey") != null ? item.Attribute("OtherKey").Value : "",
      48 ThisKey = item.Attribute("ThisKey") != null ? item.Attribute("ThisKey").Value : "",
      49 IsForeignKey = item.Attribute("IsForeignKey") != null ? item.Attribute("IsForeignKey").Value.ToLower() == "true" : false
      50 };
      51 t.Associations.Add(a);
      52 }
      53 collection.Add(t);
      54 }
      55 return collection;
      56 }
      57
      58 #endregion
      59 }
      60 }
      61
      62
      在轉(zhuǎn)化為我們的實(shí)體類(lèi),我們剩下的就是編寫(xiě)我們的CodeSmith模板了(更多知識(shí)可以參考CodeSmith模板):

      代碼
      1 <%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." %>
      2
      3 <%@ Import NameSpace="System" %>
      4 <%@ Import NameSpace="System.Xml" %>
      5 <%@ Import NameSpace="System.Text" %>
      6 <%@ Import NameSpace="System.Collections.Generic" %>
      7 <%@ Assembly Name="DbmlToTable" %>
      8
      9 --Code By Wolf
      10
      55 <%= this.GeneratorTableSql(_DbTableCollection) %>
      56
      57
      在codeSimth中我們建立了一個(gè)集合屬性傳遞實(shí)體類(lèi)DBTable和一個(gè)轉(zhuǎn)化TSql輔助方法.

      在控制臺(tái)調(diào)用編譯模板以及輸出:
      代碼
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5
      6 namespace DbmlToTable
      7 {
      8 class Program
      9 {
      10 static void Main(string[] args)
      11 {
      12 IDbTableCollectionHelper helper = new DbTableCollectionHelper();
      13 List collection = helper.
      14 Transport(System.Xml.Linq.XElement.
      15
      16 Load(@"xxpath\MultipleDocument.Data\MultipleDocumentDB.dbml"));
      17
      18 CodeSmith.Engine.CodeTemplate template = CodeSimthTemplateHelper.
      19 CompileTemplate(@"DBMLToTable.cst", w => Console.WriteLine(w));
      20 if (template != null)
      21 {
      22 CodeSimthTemplateHelper.AddPropertyParams(template, new { DbTableCollection = collection });
      23 string str = template.RenderToString();
      24 Console.WriteLine(str);
      25 //System.IO.File.AppendAllText(@"D:\1.sql", str);
      26 }
      27 Console.Read();
      28 }
      29
      30 }
      31
      32
      33
      34
      在CodeSimth中就是這么簡(jiǎn)單,生成相應(yīng)的模板代碼(個(gè)人理解CodeSmith就是把代碼作為字符串輸出)。

      在上面到我的CodeSmith模板編譯輔助類(lèi),在上一篇通過(guò)代碼生成機(jī)制實(shí)現(xiàn)強(qiáng)類(lèi)型編程-CodeSmith版也有,在這里也附帶上:需要引用CodeSmith.Engine.dll.

      代碼
      1 using System;
      2
      3 using System.Collections.Generic;
      4
      5 using System.Linq;
      6
      7 using System.Text;
      8
      9 using CodeSmith.Engine;
      10
      11 using Wolf.NameValueDictionary;
      12
      13 namespace DbmlToTable
      14
      15 {
      16
      17 public class CodeSimthTemplateHelper
      18
      19 {
      20
      21 public static CodeTemplate CompileTemplate(string templateName, Action errorWriter)
      22
      23 {
      24
      25 CodeTemplateCompiler compiler = new CodeTemplateCompiler(templateName); compiler.Compile();
      26
      27 if (compiler.Errors.Count == 0)
      28
      29 {
      30
      31 return compiler.CreateInstance();
      32
      33 }
      34
      35 else
      36
      37 {
      38
      39 for (int i = 0; i < compiler.Errors.Count; i++)
      40
      41 {
      42
      43 errorWriter(compiler.Errors[i].ToString());
      44
      45 }
      46
      47 return null;
      48
      49 }
      50
      51 }
      52
      53
      54
      55 public static void AddPropertyParams(CodeTemplate template,object param)
      56
      57 {
      58
      59 NameValueDictionary dict = new NameValueDictionary(param);
      60
      61 AddPropertyParams(template, dict);
      62
      63 }
      64
      65
      66
      67 public static void AddPropertyParams(CodeTemplate template, NameValueDictionaryparam)
      68
      69 {
      70
      71 NameValueDictionarydict = new NameValueDictionary(param);
      72
      73 foreach (var item in dict.Keys)
      74
      75 {
      76
      77 template.SetProperty(item, dict[item]);
      78
      79 }
      80
      81 }
      82
      83 }
      84
      85 }
      86

        相關(guān)評(píng)論

        閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過(guò)難過(guò)
        • 5 囧
        • 3 圍觀圍觀
        • 2 無(wú)聊無(wú)聊

        熱門(mén)評(píng)論

        最新評(píng)論

        發(fā)表評(píng)論 查看所有評(píng)論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)