part 3 kết nối với cơ sở dữ liệu database c# thêm 1 học sinh vo database

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace Ket_noi_co_so_du_kieu_database_part2 { class Program { static void Main(string[] args) {// đầu tiên ta phải khai báo kết nối với cơ sở dữ liệu SqlConnection connet = new SqlConnection(@"Data Source=QGPDGJK8S6DWF6S;Initial Catalog=Qlhocsinh;Integrated Security=True");///// cái này nằm trong hệ thống lun nha! Console.Write("\nDang ket noi du lieu voi data base!"); try { Console.Write("\nKet noi thanh cong!"); connet.Open();// mở kết nối // tạo câu lệnh thực thi đếm tất cả số lượng học sinh có trong bản hoc sinh nằm trong cở sở dữ liệu database string diemsoluongsinhvien = @"select COUNT(*) from HocSinh hs ";/// string them1hocsinh = @"INSERT INTO HocSinh (Mahocsinh,Tenhocsinh,Malop,Mamon,Diem)values ('98',N'khùng','d8','c#','9.7')"; ////////////////////////// thêm thằng học sinh có mã là 98 tên là khùng , mã lớp d8 mã môn c# điễm số 9.7 vào tren database SqlCommand cmd = new SqlCommand(diemsoluongsinhvien, connet); SqlCommand cmd2 = new SqlCommand(them1hocsinh, connet); // Console.Write("len dang thuc thi:",+cmd2.CommandText); Console.Write("\nSo luong sinh vien co trong database la: "+cmd.ExecuteScalar());// sau khi them thi đếm số lượng học sinh lại xem nó còn bao nhiêu Console.Write("\nThem 1 thong tin sinh vien!"+cmd2.ExecuteNonQuery());/// thao tác các phuong thức mới sử dụng cái này Console.Write("\nthem thanh cong!"); } catch(SqlException e) { Console.WriteLine(e.ToString()); } finally { connet.Close(); Console.Write("\nDong ket noi!"); } Console.ReadKey();//dừng nàm hình } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.