• <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 閱讀(593) 評論(0)  編輯 收藏 引用
            国内精品久久久久影院亚洲| 亚洲AV无码1区2区久久| 久久综合久久综合九色| 狠狠色丁香婷婷综合久久来来去| 亚洲国产精品久久久久婷婷老年 | 国产成人精品久久亚洲| 亚洲乱码精品久久久久..| 久久精品国产一区| 亚洲成色www久久网站夜月| 国产精品福利一区二区久久| 99久久精品免费看国产一区二区三区 | 久久只有这精品99| 99久久精品免费看国产免费| 亚洲国产天堂久久综合| 国产成人精品久久亚洲高清不卡 | 日韩电影久久久被窝网| 国产精品欧美久久久久无广告 | 久久婷婷五月综合97色一本一本| 久久伊人影视| 久久中文字幕视频、最近更新 | 久久久久久伊人高潮影院 | 久久久久久亚洲精品成人| 久久人妻AV中文字幕| 免费观看久久精彩视频| 亚洲中文字幕无码久久2020| 久久人妻少妇嫩草AV无码蜜桃| 国产精品一久久香蕉国产线看| 久久人做人爽一区二区三区| 久久亚洲中文字幕精品一区四| 国产精品99久久久久久董美香| 久久婷婷五月综合97色一本一本 | 久久水蜜桃亚洲av无码精品麻豆| 久久人妻少妇嫩草AV蜜桃| 曰曰摸天天摸人人看久久久| 久久偷看各类wc女厕嘘嘘| 亚洲va中文字幕无码久久| 一本色综合网久久| 囯产极品美女高潮无套久久久| 一本大道久久东京热无码AV| 久久天天躁狠狠躁夜夜2020老熟妇 | 久久久国产精华液|