public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = "Data Source=LAPTOP-QE71AUL8;Initial Catalog=Northwind;Integrated Security=True;Encrypt=False";
            cn.Open();
            SqlTransaction t = cn.BeginTransaction(IsolationLevel.ReadUncommitted);

            SqlCommand cmd = new SqlCommand(
                     "select * from Products where productId = 1", cn, t
                );
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            label3.Text = $"現有存量:{dr["unitsInStock"]}。";
            dr.Close();
            t.Commit();
            cn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = "Data Source=LAPTOP-QE71AUL8;Initial Catalog=Northwind;Integrated Security=True;Encrypt=False";
            cn.Open();
            SqlTransaction t = cn.BeginTransaction(IsolationLevel.ReadUncommitted);

            SqlCommand cmd = new SqlCommand(
                     "select * from Products where productId = 1", cn, t
                );
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            int qty = Convert.ToInt32(dr["unitsInStock"]);
            qty -= 1;
            dr.Close();

            //System.Threading.Thread.Sleep(1000 * 15);

            SqlCommand cmdUpdate = new SqlCommand(
                $"update products set unitsInStock = {qty} where productId = 1",
                cn,
                t
            );
            cmdUpdate.ExecuteNonQuery();
            t.Commit();
            cn.Close();

            button2.Text = qty.ToString();
        }
    }

新資料庫的程式代碼優先 - EF6 | Microsoft Learn

DBcontext介紹與Entity Framework

Codefirst與DataBaseFirst

image.png

image.png

image.png

檢查完App.config設定檔之後找出連線字串<NEW一個類別測試

image.png

最後會長這樣

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

namespace DatabaseFirstSample
{
    internal class Program
    {

        static void Main(string[] args)
        {
            // 從 app.config 中獲取連線字串名稱,建立一個新的資料庫連線
            DatabaseFirstDbEntities1 db = new DatabaseFirstDbEntities1();

            Console.WriteLine("enter a new blog name");

            // 用 var 定義變數,接收使用者輸入的 blog 名稱
            var name = Console.ReadLine();

            // 新建一個 Blog 物件,將 name 賦值給 Name 欄位
            var blog = new Blog { Name = name };

            // 使用 db 連線,將新建的 blog 加入到 Blogs 資料表中
            db.Blogs.Add(blog);

            // 儲存變更到資料庫
            db.SaveChanges();

            Console.WriteLine("ok");

            // 使用 LINQ 查詢 Blogs 資料表,依 Name 欄位排序
            var query = from b in db.Blogs
                        orderby b.Name
                        select b;

            Console.WriteLine("All blogs in the database:");

            // 輸出查詢結果中的每個 blog 的名稱
            foreach (var item in query)
            {
                Console.WriteLine(item.Name);
            }
        }

    }
}

北風的FORM,今天下午上的四個功能

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ADOLINQ20240918
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 建立 NorthwindEntities 連線,當作 DB context
        NorthwindEntities db = new NorthwindEntities();

        // 當按下 button1 時執行的事件
        private void button1_Click(object sender, EventArgs e)
        {
            // 從 Products 資料表中取得所有產品,並轉為 List 型態
            List<Product> prodList = db.Products.ToList();

            // 將產品清單設置為 dataGridView1 的資料來源,顯示在表格中
            dataGridView1.DataSource = prodList;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            db.SaveChanges();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // 新建一個 Product 物件,並設定 ProductName 和 Discontinued 欄位的初始值
            Product NewProd = new Product()
            {
                ProductName = "TempLab",  // 設定產品名稱
                Discontinued = false,     // 設定產品為未停產
            };

            // 將新產品加入 Products 資料表中
            db.Products.Add(NewProd);

            // 儲存變更到資料庫
            db.SaveChanges();
        }
        private void button4_Click(object sender, EventArgs e)
        {
            // 使用 Find 方法查詢產品編號為 1 的產品
            Product prod = db.Products.Find(1);

            // 修改該產品的庫存數量
            prod.UnitsInStock = 1111;

            // 儲存變更到資料庫
            db.SaveChanges();

            // 將按鈕文字設為 "OK"
            button4.Text = "OK";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // 使用 Find 方法查詢產品編號為 81 的產品
            Product prod = db.Products.Find(81);

            // 從 Products 資料表中移除該產品
            db.Products.Remove(prod);

            // 儲存變更到資料庫
            db.SaveChanges();

            // 將按鈕文字設為 "OK"
            button5.Text = "OK";
        }

    }
}