Insert









How do I use Insert
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: ClassBLL.cs Copy
15         public void ChuanBi3Table(string MaHK)
16         {
17             //Table 1
18             string sql1 = " select MaSV,'sotinchi'=sum(SoTC) " +
19                        " from KetQua kq,HocPhan hp,NhomHocPhan nhp,KeHoachGiangDay khgd " +
20                        " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khgd.MaKHGD=nhp.MaKHGD and MaHK='" + MaHK + "' " +
21                        " group by MaSV ";
22             DataTable dt1 = da.GetTable(sql1);
23             for (int i = 0; i < dt1.Rows.Count; i++)
24             {
25                 string sql_1 = "Insert into Loptc values('" + dt1.Rows[i][0] + "'," + dt1.Rows[i][1] + ")";
26                 da.ExcuteNonQuery(sql_1);
27             }
28             //Table 2
29             string sql2 = "select MaSV,'cuoiky'=sum(DiemTk10*SoTC) " +
30                            " from KetQua kq,NhomHocPhan nhp,HocPhan hp,KeHoachGiangDay khdt " +
31                            " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khdt.MaKHGD=nhp.MaKHGD and MaHK='" + MaHK + "' " +
32                            " group by MaSV ";
33             DataTable dt2 = da.GetTable(sql2);
34             for (int i = 0; i < dt2.Rows.Count; i++)
35             {
36                 string sql_2 = "Insert into Lopdiem10 values('" + dt2.Rows[i][0] + "'," + dt2.Rows[i][1] + ")";
37                 da.ExcuteNonQuery(sql_2);
38             }
39             //Table 3
40             string sql3 = " select Lopdiem10.MaSV,'TrungBinhCuoiKyHe10'=cuoiky/sotinchi " +
41                            " from Lopdiem10 left join Loptc on Loptc.MaSV=Lopdiem10.MaSV";
42             DataTable dt3 = da.GetTable(sql3);
43             for (int i = 0; i < dt3.Rows.Count; i++)
44             {
45                 string sql_3 = "Insert into DTB values('" + dt3.Rows[i][0] + "'," + dt3.Rows[i][1] + ")";
46                 da.ExcuteNonQuery(sql_3);
47             }
48         }
File name: ClassBLL.cs Copy
49         public void ChuanBi3Table()
50         {
51             //Table 1
52             string sql1 = " select MaSV,'sotinchi'=sum(SoTC) " +
53                        " from KetQua kq,HocPhan hp,NhomHocPhan nhp,KeHoachGiangDay khgd " +
54                        " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khgd.MaKHGD=nhp.MaKHGD " +
55                        " group by MaSV ";
56             DataTable dt1 = da.GetTable(sql1);
57             for (int i = 0; i < dt1.Rows.Count; i++)
58             {
59                 string sql_1 = "Insert into Loptc values('" + dt1.Rows[i][0] + "'," + dt1.Rows[i][1] + ")";
60                 da.ExcuteNonQuery(sql_1);
61             }
62             //Table 2
63             string sql2 = "select MaSV,'cuoiky'=sum(DiemTk10*SoTC) " +
64                            " from KetQua kq,NhomHocPhan nhp,HocPhan hp,KeHoachGiangDay khdt " +
65                            " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khdt.MaKHGD=nhp.MaKHGD " +
66                            " group by MaSV ";
67             DataTable dt2 = da.GetTable(sql2);
68             for (int i = 0; i < dt2.Rows.Count; i++)
69             {
70                 string sql_2 = "Insert into Lopdiem10 values('" + dt2.Rows[i][0] + "'," + dt2.Rows[i][1] + ")";
71                 da.ExcuteNonQuery(sql_2);
72             }
73             //Table 3
74             string sql3 = " select Lopdiem10.MaSV,'TrungBinhCuoiKyHe10'=cuoiky/sotinchi " +
75                            " from Lopdiem10 left join Loptc on Loptc.MaSV=Lopdiem10.MaSV";
76             DataTable dt3 = da.GetTable(sql3);
77             for (int i = 0; i < dt3.Rows.Count; i++)
78             {
79                 string sql_3 = "Insert into DTB values('" + dt3.Rows[i][0] + "'," + dt3.Rows[i][1] + ")";
80                 da.ExcuteNonQuery(sql_3);
81             }
82         }
File name: ClassBLL.cs Copy
275         public bool Proc_Insert(string proc,string test)
276         {
277             SqlConnection con = da.GetConnect();
278             con.Open();
279             SqlCommand cmd = new SqlCommand(proc, con);
280             cmd.CommandType = CommandType.StoredProcedure;
281             cmd.Parameters.Add("@test", SqlDbType.NVarChar).Value = test;
282             SqlDataReader dr = cmd.ExecuteReader();
283             if (dr.Read())
284             {
285                 return false;
286             }
287             return true;
288         }
File name: ClassBLL.cs Copy
302         public void Insert(string table,string sql)
303         {
304             sql = "insert into "+table+" values("+sql+")";
305             da.ExcuteNonQuery(sql);
306         }
File name: FormGiangVien.cs Copy
40         private void btnThem_Click(object sender, EventArgs e)
41         {
42             if (txtMaGV.Text!="")
43             {
44                 if(business.Proc_Insert("Insert_GiangVien",txtMaGV.Text))
45                 {
46                     string sql = "'"+txtMaGV.Text+"','"+txtTenGV.Text+"','"+dtpNamSinh.Text+"','"+txtDiaChi.Text+"'";
47                     business.Insert("GiangVien",sql);
48                     FormGiangVien_Load(sender,e);
49                 }
50                 else
51              {
52                     MessageBox.Show("Mã GV đã tồn tại");
53              }
54             }
55         }
File name: FormHeDaoTao.cs Copy
37         private void btnThem_Click(object sender, EventArgs e)
38         {
39             if(txtMaHeDT.Text!="")
40             {
41                 if (business.Proc_Insert("Insert_HeDaoTao", txtMaHeDT.Text))
42                 {
43                     string sql = "'" + txtMaHeDT.Text + "','" + txtTenHeDT.Text + "'";
44                     business.Insert("HeDaoTao", sql);
45                     FormHeDaoTao_Load(sender, e);
46                 }
47                 else
48                 {
49                     MessageBox.Show("Mã Hệ Đào Tạo đã tồn tại");
50                 }
51             }
52         }
File name: FormHocKy.cs Copy
52         private void btnThem_Click(object sender, EventArgs e)
53         {
54             if (txtMaHocKy.Text!="")
55             {
56                 if (business.Proc_Insert("Insert_HocKy", txtMaHocKy.Text))
57                 {
58                     string sql = "'" + txtMaHocKy.Text + "','" + txtTenHocKy.Text + "','" + txtNamHoc.Text + "','" + dtpBatDau.Text + "','" + dtpKetThuc.Text + "'";
59                     business.Insert("HocKy", sql);
60                     FormHocKy_Load(sender, e);
61                 }
62                 else
63                 {
64                     MessageBox.Show("Mã Học Kỳ đã tồn tại");
65                 }
66             }
67         }
File name: FormHocPhan.cs Copy
25         private void btnThem_Click(object sender, EventArgs e)
26         {
27             if (txtMaHP.Text != "")
28             {
29                 if (business.Proc_Insert("Insert_HocPhan", txtMaHP.Text))
30                 {
31                     string sql = "'" + txtMaHP.Text + "','" + txtTenHP.Text + "'," + Convert.ToInt32(txtSoTC.Text) + "," + Convert.ToInt32(txtSoTietTH.Text) + "," + Convert.ToInt32(txtSoTietLT.Text) + "," + Convert.ToInt32(txtPhanTramDQT.Text) + "," + Convert.ToInt32(txtPhanTramDT.Text) + "";
32                     business.Insert("HocPhan",sql);
33                     FormHocPhan_Load(sender,e);
34                 }
35                 else
36                 {
37                     MessageBox.Show("Mã Học Phần đã tồn tại");
38                 }
39             }
40         }
File name: FormKHGD.cs Copy
34         private void btnThem_Click(object sender, EventArgs e)
35         {
36             if (txtMaKHGD.Text != "")
37             {
38                 if (business.Proc_Insert("Insert_KHGD",txtMaKHGD.Text))
39                 {
40                     string sql = "'"+txtMaKHGD.Text+"','"+cmbMahocKy.SelectedValue.ToString()+"','"+cmbMaKhoiLop.SelectedValue.ToString()+"'";
41                     business.Insert("KeHoachGiangDay",sql);
42                     FormKHGD_Load(sender, e);
43                 }
44                 else
45                 {
46                     MessageBox.Show("Mã KHGD đã tồn tại rồi");
47                 }
48             }
49         }
File name: FormKhoiLop.cs Copy
43         private void btnThem_Click(object sender, EventArgs e)
44         {
45             if(txtMaKhoiLop.Text!="")
46             {
47                 if(business.Proc_Insert("Insert_KhoiLop",txtMaKhoiLop.Text))
48                 {
49                     string sql = "'" + txtMaKhoiLop.Text + "','" + txtTenKhoiLop.Text + "','" + cmbMaHeDT.SelectedValue.ToString() + "','" + cmbMaHeDT.SelectedValue.ToString() + "'";
50                     business.Insert("KhoiLop", sql);
51                     FormKhoiLop_Load(sender, e);
52                 }
53                 else
54                 {
55                     MessageBox.Show("Mã Khối Lớp đã tồn tại rồi");
56                 }
57             }
58         }

Download file with original file name:Insert

Insert 128 lượt xem

Gõ tìm kiếm nhanh...