2006年11月10日 DAL & ID

        2006年11月10日 星期五 天氣多雲         Last Modify: Nov 10 2006

==================================================================================

    努力的看教程中……

沒想到所謂的DAL(Data Access Layer,數據抽象層)在.net 2.0里居然就是一個DataSet強類型數據定義得XML文件(XSD後綴)…… .net 2.0 可以爲你方便的生成對應的TableAdapters命名空間和與XSD文件同名的類,這樣在商務層和表現曾總可以方便快捷的調用,他舉的例子只寫了3行代碼就實現了2層架構……

    在這個類中DotNetFramework還幫助你實現了一些有用的成員,比如果XSD文件名+Row,可以獲得對應的行,真實太無敵了,雖然知道實現起來很簡單……

用批量的方法(不選擇哪個爲單個語句生成SQL 語句,use the batch update pattern )來修改數據庫:

    NorthwindTableAdapters.ProductsTableAdapter productsAdapter =

     new NorthwindTableAdapters.ProductsTableAdapter();

 

    // For each product, double its price if it is not discontinued and

    // there are 25 items in stock or less

    Northwind.ProductsDataTable products = productsAdapter.GetProducts();

    foreach (Northwind.ProductsRow product in products)

     if (!product.Discontinued && product.UnitsInStock <= 25)

         product.UnitPrice *= 2;

 

    // Update the products

    productsAdapter.Update(products);

 

如果打上那個勾,修改單條記錄:

    NorthwindTableAdapters.ProductsTableAdapter productsAdapter = new NorthwindTableAdapters.ProductsTableAdapter();

 

    // Delete the product with ProductID 3

    productsAdapter.Delete(3);

 

    // Update Chai (ProductID of 1), setting the UnitsOnOrder to 15

    productsAdapter.Update("Chai", 1, 1, "10 boxes x 20 bags",

     18.0m, 39, 15, 10, false, 1);

 

    // Add a new product

    productsAdapter.Insert("New Product", 1, 1,

     "12 tins per carton", 14.95m, 15, 0, 10, false);

 

在插入數據庫新的元組的時候我們往往希望他能夠返回對應的新生成的ID(如插入了一個新的產品,需要獲得對應的ID),我們不要在SQL語句中使用@@Identity,而要使用scope_identity()方法,原因如下:

http://weblogs.sqlteam.com/travisl/archive/2003/10/29/405.aspx

    @@Identity

    Many TSQL books show you how to use @@Identity to get the identity of the most recently added row. Many articles online, or in magazines show the same. What you might not know is that it is potentially a source for some very hard to trace bugs in your application.

 

    @@Identity is potentially a very, very bad thing! In almost every case, you should use scope_identity() instead.

 

    Why? @@Identity returns the most recently created identity for your current connection. When you first use it, it might be fine. Until someone adds a trigger. If the trigger causes another identity to be created, guess which identity you'll get in your call to @@Identity? Not nice.

 

    scope_identity() is much nicer. It gives you what you're expecting.

====================================================================

SQL Server 2005 聯機叢書

http://msdn2.microsoft.com/zh-cn/library/ms190315.aspx

SCOPE_IDENTITY (Transact-SQL)

 

返回插入到同一作用域中的標識列內的最後一個標識值。一個範圍是一個模塊:存儲過程、觸發器、函數或批處理。因此,如果兩個語句處於同一個存儲過程、函數或批處理中,則它們位於相同的作用域中。

 

 

語法

 

SCOPE_IDENTITY()

 

備註

 

    SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY 是相似的函數,因爲它們都返回插入到標識列中的值。

 

    IDENT_CURRENT 不受作用域和會話的限制,而受限於指定的表。IDENT_CURRENT 返回爲任何會話和作用域中的特定表所生成的值。有關詳細信息,請參閱 IDENT_CURRENT (Transact-SQL)。

 

    SCOPE_IDENTITY 和 @@IDENTITY 返回在當前會話中的任何表內所生成的最後一個標識值。但是,SCOPE_IDENTITY 只返回插入到當前作用域中的值;@@IDENTITY 不受限於特定的作用域。

 

    例如,有兩個表 T1 和 T2,並且在 T1 上定義了 INSERT 觸發器。當將某行插入 T1 時,觸發器被激發,並在 T2 中插入一行。 該方案演示了兩個作用域:在 T1 上的插入,以及在 T2 通過觸發器的插入。

 

    假設 T1 和 T2 都有標識列,@@IDENTITY 和 SCOPE_IDENTITY 將在 T1 上的 INSERT 語句的最後返回不同的值。@@IDENTITY 將返回在當前會話中的任何作用域內插入的最後一個標識列的值。這是在 T2 中插入的值。SCOPE_IDENTITY() 將返回在 T1 中插入的 IDENTITY 值。這是在同一個作用域內發生的最後的插入。如果在任何 INSERT 語句作用於作用域中的標識列之前調用 SCOPE_IDENTITY() 函數,則該函數將返回空值。

 

    如果語句和事務失敗,它們會更改表的當前標識,從而使標識列中的值出現不連貫現象。即使未提交試圖向表中插入值的事務,也永遠無法回滾標識值。例如,如果因 IGNORE_DUP_KEY 衝突而導致 INSERT 語句失敗,表的當前標識值仍然會增加。

 

返回類型

 

numeric

 

示例

 

以下示列創建兩個表,TZ 和 TY,並在 TZ 中創建一個 INSERT 觸發器。當將某行插入表 TZ 中時,觸發器 (Ztrig) 將激發並在 TY 中插入一行。

 

    USE tempdb

    GO

    CREATE TABLE TZ (

     Z_id int IDENTITY(1,1)PRIMARY KEY,

     Z_name varchar(20) NOT NULL)

 

    INSERT TZ

     VALUES ('Lisa')

    INSERT TZ

     VALUES ('Mike')

    INSERT TZ

     VALUES ('Carla')

 

    SELECT * FROM TZ

 

    --Result set: This is how table TZ looks.

    Z_id Z_name

    -------------

    1 Lisa

    2 Mike

    3 Carla

 

    CREATE TABLE TY (

     Y_id int IDENTITY(100,5)PRIMARY KEY,

     Y_name varchar(20) NULL)

 

    INSERT TY (Y_name)

     VALUES ('boathouse')

    INSERT TY (Y_name)

     VALUES ('rocks')

    INSERT TY (Y_name)

     VALUES ('elevator')

 

    SELECT * FROM TY

    --Result set: This is how TY looks:

    Y_id Y_name

    ---------------

    100 boathouse

    105 rocks

    110 elevator

 

    /*Create the trigger that inserts a row in table TY

    when a row is inserted in table TZ*/

    CREATE TRIGGER Ztrig

    ON TZ

    FOR INSERT AS

     BEGIN

     INSERT TY VALUES ('')

     END

 

    /*FIRE the trigger and determine what identity values you obtain

    with the @@IDENTITY and SCOPE_IDENTITY functions.*/

    INSERT TZ VALUES ('Rosalie')

 

    SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]

    GO

    SELECT @@IDENTITY AS [@@IDENTITY]

    GO

 

 

下面是結果集:

 

    SCOPE_IDENTITY

    4

    /* SCOPE_IDENTITY returned the last identity value in the same scope. This was the insert on table TZ.*/

 

    @@IDENTITY

    115

    /* @@IDENTITY returned the last identity value inserted to TY by the trigger. This fired because of an earlier insert on TZ */

 

Note:

Make sure that you end the INSERT statement with a semi-colon before adding the SELECT statement or the new method doesn't have a parameter for each column in the Products table.

 

一般來說我們插入數據返回的都是受影響的行數,這次是ID,所以要把ExecuteNonQuery改爲executeScale

 

        Rev 1.0        Creat Document Nov 10 2006

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章