[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

Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array

Day01 聊聊對話機器人與LINE Developers

Day01 聊聊對話機器人與LINE Developers

SQL 常用語法

SQL 常用語法


Comments