Tạo ứng dụng Windows Application.
Chú ý: Trong form phải using namespace System.Data.SqlClient để có thể kết nối với CSDL SQL Server
Bước 1: Tạo đối tượng SqlConnection để kết nối với CSDL
SqlConnection connection = new SqlConnection("server=HUNGCUONG\\SQLEXPRESS;database=winform;uid=sa;pwd=123456");
//Buoc 2: Tao doi tuong SqlDataAdapter
SqlDataAdapter adapter = new SqlDataAdapter("select * from nhanvien", connection);
//Buoc 3: Tao DataSet
DataSet dsVoLam = new DataSet();
//Buoc 4: Fill du lieu vao DataSet
adapter.Fill(dsVoLam, "TonyHungThang");
//Buoc 5: Fill du lieu vao DataGridView
Từ cửa sổ ToolBox, kéo thả control DataGridView vào form, sau đó binding dữ liệu vào control này.
dgrvwVoLam.DataSource = dsVoLam.Tables["TonyHungThang"];
Sau đó chạy chương trình để xem kết quả
Bài tập:
One of the popular gyms of the city ‘FIGHTMAN’ has decided to create an information system to display each of the equipment available in the gym. This information system will help people to know about the equipments, their names and also their uses.
You need to create an application using Windows Forms with C# language to display the information. A Form should contain a Label, RichTextBox, a button and a Picture box to display image and description of the equipment. The equipments mentioned below should be displayed on the form:
Cardio, Spinner bikes, Treadmill machine etc. User should be allowed to navigate by clicking on button. (See Fig: 1)