[C#] Mssql, Ado.Net 最基本範例


Posted by mike-hsieh on 2023-09-15

簡單寫一個最簡易Ado.net的資料庫連接,來源是北風資料庫

private DataTable GetOrdersDataFromDatabase()
{
    // 連接到數據庫,查詢Orders表,返回數據
    string connectionString = "Data Source=.;Initial Catalog=NorthWind;Integrated Security=SSPI;";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        string query = "SELECT * FROM Orders";
        SqlCommand command = new SqlCommand(query, connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataTable dataTable = new DataTable();
        adapter.Fill(dataTable);
        return dataTable;
    }
}

#ado.net #MSSQL #Northwind #C#







Related Posts

[ week 2 ]  打造 JaveScript 的基礎 - 基本運算 &位元運算

[ week 2 ] 打造 JaveScript 的基礎 - 基本運算 &位元運算

[python] 關於python三兩事 - class,  __init__,   __call__

[python] 關於python三兩事 - class, __init__, __call__

模組化 (Module):require 和 export

模組化 (Module):require 和 export


Comments