ADO.NET學習心得《一》

      你們好,我是代號六零一,很高興又開始重啓博客了,爲了更好的加深本身的記憶和複習,今天開始堅持寫寫心得體會,剛開始學習ADO.NET的時候也是一臉懵逼的,代碼只有動手敲打纔會知道其實並不難,只要多敲幾遍,天然而然就會理解其中的奧妙,ADO.NET其實應用在C#中的ASP.NET及MVC項目均可以,如下是創建過程。數據庫

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ADO.NET鏈接
{
    class Program
    {
        static void Main(string[] args)
        {
            //鏈接數據庫步驟:
            //一、建立鏈接字符串
            /*方式一:Data Source=服務器名稱/./Localhost
             * Initial Catalog=itcast2014;(數據庫名稱)
             * Integrated Security=True;(Windows服務登錄)
             * 方式二:
             * string conn="Data Source=服務器名稱;Initial Catalog=數據庫名;User ID=sa;Password=123";//(SQL Service驗證登錄)
             * 方式三:server=服務器名稱/./Localhost;database=數據庫名;uid=sa;pwd=123;(SQL Service驗證登錄)
             */
            string conn = "Data Source=WIN-ERSP4OF47I2;Initial Catalog=MyDatabaseOne;User ID=sa;Password=016676";
            //二、建立鏈接對象
            using (SqlConnection connString = new SqlConnection(conn))
            {
                //測試,打開鏈接
                //三、打開鏈接(若是打開數據庫鏈接沒有問題,表示鏈接成功!)
                connString.Open();
                Console.WriteLine("打開數據庫成功");
                
                //四、關閉鏈接,釋放資源
                //connString.Close();

                //connString.Dispose();
     
            }
            Console.WriteLine("關閉鏈接,釋放資源!!");
            Console.ReadKey();
        }
    }
}
相關文章
相關標籤/搜索