資料庫連線(自動管理連線資源)
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["XXX"].ConnectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand(DBQuese, connection))
{
cmd.ExecuteNonQuery();
}
}
//或
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection()) {
using (SqlCommand cmd = new SqlCommand(sSql, conn)) {
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(dt);
}
}
}
使用 DataReader 擷取資料https://docs.microsoft.com/zh-tw/dotnet/framework/data/adonet/retrieving-data-using-a-datareader
reader.GetString(reader.GetOrdinal("body"));