[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

初識 shell script

初識 shell script

[筆記]認識 this 和 call、apply、bind

[筆記]認識 this 和 call、apply、bind

0 - 0.5 的實體伺服器架設

0 - 0.5 的實體伺服器架設


Comments