MIS2000 Lab. -- ASP.NET学习&分享 / ASP.NET案例精编(清华大学出版社)
您好,我来自台湾。很高兴与各位分享一些成果。希望对您有帮助。出版书籍是「ASP.NET案例精编 / 清华大学出版社」。

这里是我的文章备份

原文请看:

http://www.dotblogs.com.tw/mis2000lab/archive/2011/10/14/code_compare_dataset_linq_entity.aspx

DataSet / LinqDataSource / EntityDataSource(查询产生器)三种方式,在数据新增上的异同。Comparing the difference of Code-Behind about DataSet and LinqDataSource and EntityDataSource to Insert DB a new Record.

 

 

 

 

主題:   DataSet / LinqDataSource / EntityDataSource(查询产生器)三种方式,在数据新增上的异同。

Title :   Comparing the difference of Code-Behind about DataSet and LinqDataSource and EntityDataSource to Insert DB a new Record.

 

 

這些範例都在書本「下集」,

我只是彙整起來作一個比較,希望讀者比較清楚三種寫法的差異。

My book has published these samples.

Now, I comare these 3 samples and try to explain the difference for coding in DataSet, LinqDataSource and EntityDataSource.

 

 

請先參閱以前的文章:

Before you reading, wish you can study the older articles that I've published.

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

(1). DataSet如何新增一筆紀錄?(搭配參數InsertCommand + Parameter)

How to Insert a new record in DataBase (using DataAdapter with InsertCommand and Parameters)

程式碼(Code):http://www.dotblogs.com.tw/mis2000lab/archive/2008/12/15/ado.net_dataset_insertcommand_1215.aspx

觀念與圖解(Concept explain and using pictures to demo the coding flow):http://www.dotblogs.com.tw/mis2000lab/archive/2011/04/20/dataset_insert_20110420.aspx

(2). Linq 與 LinqDataSource

書本「下集」第五章的範例 Manual_05.aspx,沒有使用 LinqDataSource,都是自己手寫的程式(Code Behind)。

(3).  Entity   我採用 -- 查詢產生器方法(Query builder methods)來做

http://www.dotblogs.com.tw/mis2000lab/archive/2010/10/27/entity_manual_4_savechange.aspx

(此範例已經收錄在書本「下集」

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

 

 

我們來比對這三者的程式碼,如何新增一筆記錄到資料庫裡面?

When we code manually for inserting a new record into database,

we can choice one of them to do this.

 

These three samples demo for (1). DataSet (ADO.NET), (2). LinqDataSource and (3.)EntityDataSource (Query builder methods).

 

(1). DataSet(自己手寫 ADO.NET程式

Dim Conn As New SqlConnection("DB連結字串--Connection String")
Dim ds As New DataSet

Dim u_Adapter As New SqlDataAdapter
    u_Adapter.SelectCommand = New SqlCommand("Select * from test", Conn)
    u_Adapter.Fill(ds, "test") '---- 這時候執行SQL指令。取出資料,放進 DataSet。

Dim new_row As DataRow = ds.Tables("test").NewRow()
'-- 手動新增一行 DataRow
      new_row("class") = "科技"
      new_row("title") = "咖哩薑黃素 防失智又抗癌"
      new_row("summary") = "最近愛爾蘭的科學家實驗發現,咖哩中的薑黃素殺死癌細胞的功能..."
      new_row("article") = "食道癌高居我國癌症死亡病因第九名,不過最近愛爾蘭的科學家實驗發現,咖哩中的薑黃素殺死癌細胞的功能,在治療食道癌上有顯著效果,..."
      new_row("author") = "台灣醒報記者楊舒婷綜合報導"

ds.Tables("test").Rows.Add(new_row)   '--將新增的一行 DataRow加入 DataSet裡面


'==事先寫好 InsertCommand ===================================

u_Adapter.InsertCommand = New SqlCommand("INSERT INTO [test] ([test_time], [class], [title], [summary], [article], [author]) VALUES (getdate(), @class, @title, @summary, @article, @author)", Conn)

'-- InsertCommand 參數 --(start)
   u_Adapter.InsertCommand.Parameters.Add("@class", SqlDbType.NVarChar, 50)
   u_Adapter.InsertCommand.Parameters("@class").Value = "科技"

   u_Adapter.InsertCommand.Parameters.Add("@title", SqlDbType.NVarChar, 100)
   u_Adapter.InsertCommand.Parameters("@title").Value = "咖哩薑黃素 防失智又抗癌"

   u_Adapter.InsertCommand.Parameters.Add("@summary", SqlDbType.NVarChar, 250)
   u_Adapter.InsertCommand.Parameters("@summary").Value = "最近愛爾蘭的科學家實驗發現,咖哩中的薑黃素殺死癌細胞的功能..."

   u_Adapter.InsertCommand.Parameters.Add("@article", SqlDbType.NVarChar, 16)
   u_Adapter.InsertCommand.Parameters("@article").Value = "食道癌高居我國癌症死亡病因第九名,不過最近愛爾蘭的科學家實驗發現,咖哩中的薑黃素殺死癌細胞的功能,在治療食道癌上有顯著效果,..."

   u_Adapter.InsertCommand.Parameters.Add("@author", SqlDbType.NVarChar, 100)
   u_Adapter.InsertCommand.Parameters("@author").Value = "台灣醒報記者楊舒婷綜合報導"
'-- InsertCommand 參數 --(end)

'=====================================================


u_Adapter.Update(ds, "test")
'---- 這時候執行SQL指令。把 DataSet裡面的新資料,回寫到資料庫!

因為 DataSet + DataAdapter 會自動開啟、關閉資料庫的連線,
所以上述程式碼中,沒看見 Conn.Open() 或 Conn.Close()

 

(2). LINQ & LinqDataSource

 '== (1). 連結資料庫。Connection to DB。
Dim db As New DataContext("DB連結字串--Connection String")
'-- 使用 DataContext類別連結資料庫using DataContext class to onnect DB.


'== (2). 執行 LINQ指令,存取資料。Using DataContext's .GetTable() method to return the collection of table objects.

Dim myTest As Table(Of test) = db.GetTable(Of test)() 

'-- 必須搭配 System.Data.Linq 命名空間(NameSpace)
    '-- DataContext的 .GetTable()方法,是用來傳回表格物件的集合。
    '-- http://msdn.microsoft.com/zh-tw/library/system.data.linq.datacontext.gettable.aspx


' Create a new "test" object.
'-- 參考資料 http://msdn.microsoft.com/zh-tw/library/bb386941.aspx

Dim AddNewData As New test With _
    { .class = "科技", _
      .test_time = DateTime.Now(), _
      .title = "咖哩薑黃素 防失智又抗癌", _
      .summary = "最近愛爾蘭的科學家實驗發現,咖哩中的薑黃素殺死癌細胞的功能...", _
      .article = "食道癌高居我國癌症死亡病因第九名,不過最近愛爾蘭的科學家實驗發現,咖哩中的薑黃素殺死癌細胞的功能,在治療食道癌上有顯著效果,...", _
      .author = "台灣醒報記者楊舒婷綜合報導"  }

myTest.InsertOnSubmit(AddNewData)
    '-- 將 pending insert狀態中的實體  加入至此 Table(Of (TEntity))。
    '-- 在呼叫 SubmitChanges之前,不會在這份資料表的查詢結果中看到所加入的實體。
    '-- http://msdn.microsoft.com/zh-tw/library/bb763516.aspx


db.SubmitChanges()
    '-- 插入、更新或刪除的一組已修改的物件,並執行適當的命令來實作資料庫的變更。

    '-- Insert a new record into Database
    '-- http://msdn.microsoft.com/zh-tw/library/bb292162.aspx

 

(3). EntityDataSource      查詢產生器方法(Query builder methods)範例

Using u_context As New testEntities
    Try
        '**** 新增(Insert) ********************************(start)
        Dim AddTEST As New test()
                AddTEST.test_time = DateTime.Now().ToString()
                AddTEST.class = "科技"
                AddTEST.title = "咖哩薑黃素 防失智又抗癌"
                AddTEST.summary = "最近愛爾蘭的科學家實驗發現,咖哩中的薑黃素殺死癌細胞的功能..."
                AddTEST.article = "食道癌高居我國癌症死亡病因第九名,不過最近愛爾蘭的科學家實驗發現,咖哩中的薑黃素殺死癌細胞的功能,在治療食道癌上有顯著效果,..."
                AddTEST.author = "台灣醒報記者楊舒婷綜合報導"

        u_context.test.AddObject(AddTEST)
        '**** 新增(Insert) ********************************(end)

        Dim affectRows As Integer = u_context.SaveChanges()
        '*** 寫回資料庫裡面。Insert a new record into Database ***

        Label1.Text = "完成(Success)! --- " & affectRows
    End Try
End Using

 

以上範例的 C#語法 請參閱本書「下集」。

posted on 2011-11-04 14:57  MIS2000 Lab.  阅读(398)  评论(0)    收藏  举报


ASP.NET案例精编——适用于VS 2005/2008(配光盘)
 

当当网购买 http://product.dangdang.com/product.aspx?product_id=20583373&ref=search-1-pub