DataSet / LinqDataSource / EntityDataSource(查詢產生器)三種方式,在資料新增上的異同。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.





 

主題:   DataSet / LINQ / Entity三種方式,在資料新增上的異同

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#語法 請參閱本書「下集」。

 

 

我將思想傳授他人, 他人之所得,亦無損於我之所有;

猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson

線上課程教學,遠距教學 (Web Form 約 51hr)  https://dotblogs.com.tw/mis2000lab/2016/02/01/aspnet_online_learning_distance_education_VS2015

線上課程教學,遠距教學 (ASP.NET MVC 約 135hr)  https://dotblogs.com.tw/mis2000lab/2018/08/14/ASPnet_MVC_Online_Learning_MIS2000Lab

 

寫信給我,不要私訊 --  mis2000lab (at) yahoo.com.tw  或  school (at) mis2000lab.net

 (1) 第一天 ASP.NET MVC5 完整影片(5.5小時 / .NET 4.x版)免費試聽。影片 https://youtu.be/9spaHik87-A 

 (2) 第一天 ASP.NET Core MVC 完整影片(3小時 / .NET Core 6.0~8.0)免費試聽。影片 https://youtu.be/TSmwpT-Bx4I 

[學員感言] mis2000lab課程評價 - ASP.NET MVC , WebForm  。 https://mis2000lab.medium.com/%E5%AD%B8%E5%93%A1%E6%84%9F%E8%A8%80-mis2000lab%E8%AA%B2%E7%A8%8B%E8%A9%95%E5%83%B9-asp-net-mvc-webform-77903ce9680b  


ASP.NET遠距教學、線上課程(Web Form + MVC)。 第一天課程, "完整" 試聽。 

.........   facebook社團   https://www.facebook.com/mis2000lab   ......................

.........  YouTube (ASP.NET) 線上教學影片  https://www.youtube.com/channel/UC6IPPf6tvsNG8zX3u1LddvA/

 

Blog文章 "附的範例" 無法下載,請看 https://dotblogs.com.tw/mis2000lab/2016/03/14/2008_2015_mis2000lab_sample_download

請看我們的「售後服務」範圍(嚴格認定)。

...................................................................................................................................................... 

ASP.NET MVC  => .NET Core MVC 線上教學  ...... 第一天課程 完整內容 "免費"讓您評估 / 試聽

[遠距教學、教學影片] ASP.NET (Web Form) 課程 上線了!MIS2000Lab.主講   事先錄好的影片,並非上課側錄!   觀看時,有如「一對一」面對面講課