这里是我的文章备份
原文请看:
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") '-- InsertCommand 參數 --(start) '=====================================================
|
(2). LINQ & LinqDataSource
|
'== (1). 連結資料庫。Connection to DB。
Dim myTest As Table(Of test) = db.GetTable(Of test)() '-- 必須搭配 System.Data.Linq 命名空間(NameSpace)
'-- Insert a new record into Database |
(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#語法 請參閱本書「下集」。
....................................................................................................寄信给我 mis2000lab (at)雅虎.com.台湾 ........
ASP.NET案例精编(清华大学出版社 / 作者MIS2000Lab)
http://www.china-pub.com/46063
2009/5/15上市
市场价 :¥59.80 RMB(人民幣)
浙公网安备 33010602011771号