簡單寫一個最簡易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;
}
}