M$ cung cấp 2 cách: disconnected-enviroment và connected-enviroment
1/ disconnected: dùng dataset hoặc datatable để chứa dữ liệu: (code in C#)
Code:
string connectionString = "server=localhost; database=Northwind; uid=sa; pwd=;";
string querystring = "SELECT * FROM Employees";
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
SqlAdapter da = new SqlAdapter(querystring, cn);
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
cn.Close();
// rồi, bây giờ thì truy xuất dữ liệu trong dataset:
for (int i = 0; i < ds.Tables["Employees"].Count; i++)
{
// access vào ds.Tables["Employees"][i]["colname"];
}
2/connected: dùng datareader để duyệt dữ liệu: (code in C#)
Code:
string connectionString = "server=localhost; database=Northwind; uid=sa; pwd=;";
string querystring = "SELECT * FROM Employees";
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
SqlDataReader dr = new SqlDataReader(sql, cn);
// rồi, bây giờ thì truy xuất dữ liệu trong datareader:
while (dr.Read())
{
// access vào dr["colname"];
}
Bookmarks