• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Javen-Studio 咖啡小屋

            http://javenstudio.org - C++ Java 分布式 搜索引擎
            Naven's Research Laboratory - Thinking of Life, Imagination of Future

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              24 隨筆 :: 57 文章 :: 170 評論 :: 4 Trackbacks

            6           SQL集成(SQL Integration

            .NET 語言級集成查詢可以用來直接查詢關系型的數據存儲(relational data stores),而不用離開本地編程語言(local programming language)的語法或編譯時環境(the syntax or compile-time environment)。這個技巧(This facility),代碼名叫 DLinqcode-named DLinq),把 SQL schema 信息的集成(the integration of SQL schema information)的優勢帶入了(takes advantage of CLR 元數據(metadata)。這個集成(integration)把 SQL 表(table)和視圖(view)的定義編譯進 CLR 類型,這樣能夠從任何語言上訪問。

             

            DLinq 定義了兩個核心的屬性(two core attributes),[Table] [Column],它們指出(indicate)了哪個 CLR 類型和屬性(which CLR types and properties)是符合(correspond to)外部的 SQL 數據(external SQL data)。[Table] 屬性能被應用于(applied to)一個類并聯合(associates CLR 類型和一個指定的(named SQL 表或視圖。這兩個屬性都是參數化的(parameterized),以允許 SQL 型的元數據(SQL-specific metadata)得以保留(retained)。舉例來說,考察下面這段 SQL schema 的定義:

             

            create table People (
                Name nvarchar(
            32primary key not null
                Age 
            int not null
                CanCode bit not 
            null
            )

            create table Orders (
                OrderID nvarchar(
            32primary key not null
                Customer nvarchar(
            32) not null
                Amount 
            int
            )

             

            與之對等(equivalent)的 CLR 定義看起來如下:

             

            [Table(Name="People")]
            public class Person {
              [Column(DbType
            ="nvarchar(32) not null", Id=true)]
              
            public string Name; 

              [Column]
              
            public int Age;

              [Column]
              
            public bool CanCode;
            }


            [Table(Name
            ="Orders")]
            public class Order {
              [Column(DbType
            ="nvarchar(32) not null", Id=true)]
              
            public string OrderID; 

              [Column(DbType
            ="nvarchar(32) not null")]        
              
            public string Customer; 

              [Column]
              
            public int? Amount; 
            }

             

            通過這個例子注意到,允許為 null nullable)的 column 映射到(map toCLR 里的允許為 null 的類型(允許為 null 的類型首次出現在 .NET Framework version 2),對 SQL 類型來說沒有一個與之一一對應的(a 1:1 correspondence withCLR 類型(比如nvarchar, char, text),原始的 SQL 類型在 CLR metadata 里被保留。

             

            為發行一個查詢而不是一個關系型存儲(issue a query against a relational store),LINQ 模式的 Dlinq 實現把查詢從它的表達樹(expression tree)轉換并編成(translates …form into)一個 SQL 表達試和適合遠程賦值(suitable for remote evaluation)的 ADO.NET DbCommand 對象。例于考察如下簡單的查詢:

             

            // establish a query context over ADO.NET sql connection
            DataContext context = new DataContext(
                 
            "Initial Catalog=petdb;Integrated Security=sspi");

            // grab variables that represent the remote tables that 
            // correspond to the Person and Order CLR types
            Table<Person> custs = context.GetTable<Person>();
            Table
            <Order> orders   = context.GetTable<Order>();

            // build the query
            var query = from c in custs, o in orders
                        where o.Customer 
            == c.Name
                        select 
            new 
                                   c.Name, 
                                   o.OrderID,
                                   o.Amount,
                                   c.Age
                        }


            // execute the query
            foreach (var item in query) 
                Console.WriteLine(
            "{0} {1} {2} {3}"
                                  item.Name, item.OrderID, 
                                  item.Amount, item.Age);

             

            DataContext 類型提供了一個輕量級的轉換器(lightweight translator),它的工作是把標準查詢操作符(standard query operators)轉換成 SQLDataContext 使用現有的 ADO.NET IdbConnection 來訪問存儲(accessing the store),能夠使用一個確定(established)的ADO.NET 連接對象或者一個可以用來創建一個連接的連接字符串(a connection string)的任一個來初始化(initialized)。

             

            GetTable 方法提供 IEnumerable 兼容的變量(IEnumerable-compatible variables),能夠被用在查詢表達式(query expressions)里來代表(represent)遠程的表或視圖(the remote table or view)。調用 GetTable 不會導致任何與數據庫的交互(interaction),更準確的說(rather)它們扮演(representthe potential 通過時用查詢表達式來與遠程的表或視圖相配合(interact with)。在我們上面的例子中,查詢不會對存儲(store)發送傳輸的影響(get transmitted to),直到程序迭代出(iterates over)查詢表達式為止,在 C# 中使用 foreach 語句也是如此(in this case)。當程序開始迭代完成(iterates over)查詢時,DataContext 機構(machinery)把查詢表達式樹(expression tree)轉換成要發送給存儲(sent to the store)的如下所示的 SQL 語句:

             

            SELECT [t0].[Age], [t1].[Amount], 
                   [t0].[Name], [t1].[OrderID]
            FROM [Customers] AS [t0], [Orders] AS [t1]
            WHERE [t1].[Customer] 
            = [t0].[Name]

             

            需要重點注意的是通過在本地編程語言(local programming language)中直接(directly)內建查詢能力(building query capability),開發人員可以獲得關系模型(the relational model)的完全能力(full power),而不用不得不靜態地(statically)將關聯烘烤進(bake the relationships intoCLR 類型中。成熟的 O/R 映射(Full-blown object/relational mapping)技術還能夠為那些希望它泛函性(functionality)的用戶利用(take advantage of)這個核心查詢能力(core query capability)。

             

             

             

             

             

            待續, 錯誤難免,請批評指正,譯者Naven 2005-10-24

            posted on 2005-10-30 19:50 Javen-Studio 閱讀(597) 評論(0)  編輯 收藏 引用
            成人国内精品久久久久影院| 91久久精品国产成人久久| 日批日出水久久亚洲精品tv| 精品久久人人爽天天玩人人妻| 精品免费久久久久国产一区 | 久久久久久综合网天天| 久久无码AV一区二区三区| 麻豆AV一区二区三区久久| 人人狠狠综合久久亚洲婷婷| 人人狠狠综合久久亚洲高清| 婷婷久久香蕉五月综合加勒比| 日本免费一区二区久久人人澡| 色天使久久综合网天天| 国产91久久精品一区二区| 亚洲国产天堂久久综合| 老色鬼久久亚洲AV综合| 开心久久婷婷综合中文字幕| 国产成人精品久久一区二区三区 | 亚洲欧美一级久久精品| 熟妇人妻久久中文字幕| 国产综合成人久久大片91| 久久久噜噜噜www成人网| 久久久免费观成人影院| 99久久久国产精品免费无卡顿| 日本精品久久久久影院日本 | 99久久精品免费看国产一区二区三区 | 国产精品99久久久精品无码| 国内精品久久久久| 99精品久久久久久久婷婷| 亚洲精品国产自在久久| 亚洲午夜精品久久久久久人妖| 欧美喷潮久久久XXXXx| 亚洲精品无码成人片久久| 亚洲国产精品综合久久一线| 久久久久久亚洲精品不卡| 国产综合免费精品久久久| 久久久久夜夜夜精品国产| 久久99热狠狠色精品一区| 国产精品久久久久9999| 国产精品久久精品| 中文字幕久久欲求不满|