close

ASP.NET Core & ADO.NET 入門 #1 -- 資料庫連結(SqlConnection)

ASP.NET Core & ADO.NET 入門 #1,最簡單的操作步驟

(1).  SqlConnection連結字串

本範例提供YouTube教學影片 + PDF電子書下載

 

***  行前準備  ***

首先,您可以先瞭解 開放原始碼(開源)的 ASP.NET Core(以前名為 ASP.NET 5

      [中文翻譯] ASP.NET 5 概觀 (ASP.NET 5 Overview,原作 Tom FitzMacken 2014/11/12)

      https://dotblogs.com.tw/mis2000lab/2014/12/03/aspnet_5_overview_webform_46      

      [中文翻譯] ASP.NET 5 簡介(Introducing ASP.NET 5,原作ScottGu 2015/2/23)

      https://dotblogs.com.tw/mis2000lab/2015/03/11/aspnet_5_introduce_scottgu_20150223

 

接著,當您打開 VS 2015 (Update 3)的時候,可能要先安裝這個套件 for VS 2015。這個 Tool 還是 Preview版

      系統會自動偵測,並詢問您是否安裝?

      安裝過程,詳見 http://vmiv.blogspot.tw/2016/06/aspnet-core-10.html

 

以下的範例,是從 Wrox出版的這本書而來,您可以參閱此書的 第三十七章(原廠網站有範例下載)

   

Professional C# 6 and .NET Core 1.0

Christian Nagel

ISBN: 978-1-119-09660-3

1536 pages

April 2016......註解:提醒您,這本書以為 Windows Form 主軸,並非 網頁範例為主

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

YouTube教學影片 -- https://youtu.be/1UaO8C2MnJQ

 

 

*** 開始動作 ***

(1). VS 2015 新建一個 ASP.NET Core 的 Web專案。

      下一步,我選擇了「」專案

 

(2). 這是ASP.NET Core 「」專案的架構

很抱歉,關於這個架構的檔案、目錄,我暫時沒有研究。如果您想進一步瞭解,請參與網路上的其他文章

 

(3). 預設的狀態下,您就可以編譯這個專案,而且看見結果(執行後,網頁出現 Hello World 字樣)

對照起來,原來是在 Startup.cs這個檔案的最後下方,就有 Hello World 字樣

 

(4). 我們加入熟悉的 ADO.NET程式碼(以 DataReader + SqlConnection / SqlCommand為例)

    如果您不熟悉,可以參閱以前的文章與範例 -- 

 

這裡的變化比較大。

以前撰寫程式時,如果缺乏 NameSpace,您可能要在程式碼「最上方」自己動手寫進來

甚至.....您要使用「加入參考」把這個DLL檔(二進位檔案)加入您的網站或專案

 

但是在 ASP.NET Core,我們可以有下面兩種作法。任選其一即可。

 

4-1.    透過 Visual Studio來幫忙,自己會幫您完成。詳見下面圖解。

Visual Studio會自動幫您加上 NameSpace,也會在 project.json設定檔裡面,加入必要的元件。

 

 

4-2.    當然,如果您是死硬派!硬漢!堅持要動手寫!

          在 project.json設定檔裡面,也可以透過智慧選字(IntelliSense),會自動跳字讓您填寫。

 

(5). 程式碼大致如下:

 

                //== 方法一 ==   

                string connectionString = @"server=.\sqlexpress;integrated security=SSPI;database=我的資料庫名稱";

                var conn = new SqlConnection(connectionString);

                conn.Open();

                // Do something useful

                await context.Response.WriteAsync("

資料庫連結成功");

                conn.Close();

               //== 當然,我們可以把資料庫的連結字串,寫在設定檔裡面。例如 appsettings.json設定檔,下一篇文章 為您解說。
              //{
              //  "ConnectionStrings": {
              //    "DefaultConnection": "Server=.\\sqlexpress;Database=我的資料庫名稱;Trusted_Connection=True;MultipleActiveResultSets=true"
              //  }
              //}

 

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

 

Q :  ADO.NET怎麼只剩下 DataReader了?以前慣用的 DataSet呢?

 

詳見這篇文章的說明 (2016/2/10發表)  https://blogs.msdn.microsoft.com/dotnet/2016/02/10/porting-to-net-core/

System.Data. While the base layer is already part of .NET Core, i.e. the provider model and SQL client, some features are currently not available, such as schema support and DataTable/DataSet.

 

System.Transactions. While ADO.NET supports transactions, there is no support for distributed transactions, which includes the notion of ambient transactions and enlistment. (您可以參閱我這本書 -- 深入探索 .NET資料存取:ADO.NET + SqlDataSource+ LINQ (松崗),有討論 System.Transaction)

 

System.Net.Mail. There is currently no support for sending emails from .NET Core using these APIs.

目前 Core 1.0 不支援,不代表以後不支援。所以也不用太絕望....等等看吧......

 

下一篇文章:

ASP.NET Core & ADO.NET 入門 #2 -- 讀取appsettings.json設定檔與參數的寫法(SqlCommand)

http://mis2000lab.blogspot.tw/2016/12/aspnet-core-adonet-2.html 

 

 
 

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

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

 

寫信給我--  mis2000lab (at) yahoo.com.台灣  或是  school (at) mis2000lab.net

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

................   Google+   https://plus.google.com/100202398389206570368/posts ........

................  YouTube (ASP.NET) 線上教學影片  http://goo.gl/rGLocQ

arrow
arrow

    MIS2000 Lab 發表在 痞客邦 留言(0) 人氣()