Connect
How do I use Connect
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: ClassBLL.cs
Copy
83 public DataTable XemDiemLop(string MaLop)
84 {
85 SqlConnection con = new SqlConnection();
86 ChuanBi3Table();
87 string sql4 = "select sv.MaSV,TenSV,TrungBinhCuoiKyHe10 " +
88 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV " +
89 " where MaLop='" + MaLop + "'";
90 DataTable dt4 = da.GetTable(sql4);
91 da.ExcuteNonQuery("delete from Loptc where MaSV in ( select MaSV from Loptc )");
92 da.ExcuteNonQuery("delete from Lopdiem10 where MaSV in ( select MaSV from Lopdiem10 )");
93 da.ExcuteNonQuery("delete from DTB where MaSV in ( select MaSV from DTB )");
94 return dt4;
95 }
File name: ClassBLL.cs
Copy
96 public DataTable ThongKeLop(string MaHK,string MaLop,int loai)
97 {
98 SqlConnection con = da.GetConnect();
99 ChuanBi3Table(MaHK);
100 //Truy van cuoi cung
101 string sql4;
102 switch (loai)
103 {
104 case 0:
105 sql4 = "select sv.MaSV,TenSV,TrungBinhCuoiKyHe10 " +
106 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV " +
107 " where MaLop='" + MaLop + "' and TrungBinhCuoiKyHe10 >= 9";
108 break;
109 case 1:
110 sql4 = "select sv.MaSV,TenSV,TrungBinhCuoiKyHe10 " +
111 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV " +
112 " where MaLop='" + MaLop + "' and TrungBinhCuoiKyHe10 >=8 and TrungBinhCuoiKyHe10 < 9";
113 break;
114 case 2:
115 sql4 = "select sv.MaSV,TenSV,TrungBinhCuoiKyHe10 " +
116 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV " +
117 " where MaLop='" + MaLop + "' and TrungBinhCuoiKyHe10 >=6.5 and TrungBinhCuoiKyHe10 < 8";
118 break;
119 case 3:
120 sql4 = "select sv.MaSV,TenSV,TrungBinhCuoiKyHe10 " +
121 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV " +
122 " where MaLop='" + MaLop + "' and TrungBinhCuoiKyHe10 >=5 and TrungBinhCuoiKyHe10 < 6.5";
123 break;
124 case 4:
125 sql4 = "select sv.MaSV,TenSV,TrungBinhCuoiKyHe10 " +
126 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV " +
127 " where MaLop='" + MaLop + "' and TrungBinhCuoiKyHe10 >=3.5 and TrungBinhCuoiKyHe10 < 5";
128 break;
129 default:
130 sql4 = "select sv.MaSV,TenSV,TrungBinhCuoiKyHe10 " +
131 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV " +
132 " where MaLop='" + MaLop + "' and TrungBinhCuoiKyHe10 < 3.5";
133 break;
134 }
135 DataTable dt4 = da.GetTable(sql4);
136 da.ExcuteNonQuery("delete from Loptc where MaSV in ( select MaSV from Loptc )");
137 da.ExcuteNonQuery("delete from Lopdiem10 where MaSV in ( select MaSV from Lopdiem10 )");
138 da.ExcuteNonQuery("delete from DTB where MaSV in ( select MaSV from DTB )");
139 return dt4;
140 }
File name: ClassBLL.cs
Copy
141 public DataTable ThongKeKhoiLop(string MaHK, string MaKhoiLop, int loai)
142 {
143 SqlConnection con = da.GetConnect();
144 ChuanBi3Table(MaHK);
145 string sql4;
146 switch (loai)
147 {
148 case 0:
149 sql4 = "select sv.MaSV,TenSV,sv.MaLop,TrungBinhCuoiKyHe10 " +
150 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV,KhoiLop kl,Lop l " +
151 " where sv.MaLop=l.MaLop and l.MaKhoiLop=kl.MaKhoiLop " +
152 " and kl.MaKhoiLop='" + MaKhoiLop + "' and TrungBinhCuoiKyHe10 >=9 ";
153 break;
154 case 1:
155 sql4 = "select sv.MaSV,TenSV,sv.MaLop,TrungBinhCuoiKyHe10 " +
156 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV,KhoiLop kl,Lop l " +
157 " where sv.MaLop=l.MaLop and l.MaKhoiLop=kl.MaKhoiLop " +
158 " and kl.MaKhoiLop='" + MaKhoiLop + "' and TrungBinhCuoiKyHe10 >=8 and TrungBinhCuoiKyHe10 < 9";
159 break;
160 case 2:
161 sql4 = "select sv.MaSV,TenSV,sv.MaLop,TrungBinhCuoiKyHe10 " +
162 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV,KhoiLop kl,Lop l " +
163 " where sv.MaLop=l.MaLop and l.MaKhoiLop=kl.MaKhoiLop " +
164 " and kl.MaKhoiLop='" + MaKhoiLop + "' and TrungBinhCuoiKyHe10 >=6.5 and TrungBinhCuoiKyHe10 < 8";
165 break;
166 case 3:
167 sql4 = "select sv.MaSV,TenSV,sv.MaLop,TrungBinhCuoiKyHe10 " +
168 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV,KhoiLop kl,Lop l " +
169 " where sv.MaLop=l.MaLop and l.MaKhoiLop=kl.MaKhoiLop " +
170 " and kl.MaKhoiLop='" + MaKhoiLop + "' and TrungBinhCuoiKyHe10 >=5 and TrungBinhCuoiKyHe10 < 6.5";
171 break;
172 case 4:
173 sql4 = "select sv.MaSV,TenSV,sv.MaLop,TrungBinhCuoiKyHe10 " +
174 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV,KhoiLop kl,Lop l " +
175 " where sv.MaLop=l.MaLop and l.MaKhoiLop=kl.MaKhoiLop " +
176 " and kl.MaKhoiLop='" + MaKhoiLop + "' and TrungBinhCuoiKyHe10 >=3.5 and TrungBinhCuoiKyHe10 < 5";
177 break;
178 default:
179 sql4 = "select sv.MaSV,TenSV,sv.MaLop,TrungBinhCuoiKyHe10 " +
180 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV,KhoiLop kl,Lop l " +
181 " where sv.MaLop=l.MaLop and l.MaKhoiLop=kl.MaKhoiLop " +
182 " and kl.MaKhoiLop='" + MaKhoiLop + "' and TrungBinhCuoiKyHe10 < 3.5";
183 break;
184 }
185 DataTable dt4 = da.GetTable(sql4);
186 da.ExcuteNonQuery("delete from Loptc where MaSV in ( select MaSV from Loptc )");
187 da.ExcuteNonQuery("delete from Lopdiem10 where MaSV in ( select MaSV from Lopdiem10 )");
188 da.ExcuteNonQuery("delete from DTB where MaSV in ( select MaSV from DTB )");
189 return dt4;
190 }
File name: ClassBLL.cs
Copy
191 public int CheckLogin(string name, string pass)
192 {
193 SqlConnection con = da.GetConnect();
194 SqlCommand cmd = new SqlCommand("select *from Users Where Username='"+name+"' and Password='"+pass+"'",con);
195 con.Open();
196 SqlDataReader dr = cmd.ExecuteReader();
197 if (dr.Read())
198 {
199 if (dr[2].ToString() == "admin")
200 {
201 return 1;
202 }
203 else
204 {
205 return 2;
206 }
207 }
208 else
209 {
210 return -1;
211 }
212 }
File name: ClassBLL.cs
Copy
213 public string TinhDiemTB(string MaSV, string MaHK)
214 {
215 SqlConnection con = da.GetConnect();
216 con.Open();
217 //view tc
218 string sql1 = " select 'sotinchi'=sum(SoTC) " +
219 " from KetQua kq,HocPhan hp,NhomHocPhan nhp,KeHoachGiangDay khgd " +
220 " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khgd.MaKHGD=nhp.MaKHGD and MaSV='"+MaSV+"' and MaHK='"+MaHK+"' " +
221 " group by MaSV ";
222 SqlCommand cmd1 = new SqlCommand(sql1, con);
223 string tc = cmd1.ExecuteScalar().ToString();
224 // view diem10
225 string sql2 = " select 'cuoiky'=sum(DiemTk10*SoTC) " +
226 " from KetQua kq,NhomHocPhan nhp,HocPhan hp,KeHoachGiangDay khdt " +
227 " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khdt.MaKHGD=nhp.MaKHGD and MaSV='"+MaSV+"' and MaHK='"+MaHK+"' " +
228 " group by MaSV ";
229 SqlCommand cmd2 = new SqlCommand(sql2, con);
230 string diem10 = cmd2.ExecuteScalar().ToString();
231 //view diem4
232 string sql3 = " select 'cuoiky'=sum(DiemTk4*SoTC) " +
233 " from KetQua kq,NhomHocPhan nhp,HocPhan hp,KeHoachGiangDay khdt " +
234 " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khdt.MaKHGD=nhp.MaKHGD and MaSV='"+MaSV+"' and MaHK='"+MaHK+"' " +
235 " group by MaSV ";
236 SqlCommand cmd3 = new SqlCommand(sql3, con);
237 string diem4 = cmd3.ExecuteScalar().ToString();
238
239
240 string ketqua="";
241 ketqua = Convert.ToDouble(diem10) / Convert.ToDouble(tc) + "," + Convert.ToDouble(diem4) / Convert.ToDouble(tc) ;
242 return ketqua;
243 }
File name: ClassBLL.cs
Copy
244 public string TinhDiemTB(string MaSV)
245 {
246 SqlConnection con = da.GetConnect();
247 con.Open();
248 //view tc
249 string sql1 = " select 'sotinchi'=sum(SoTC) " +
250 " from KetQua kq,HocPhan hp,NhomHocPhan nhp,KeHoachGiangDay khgd " +
251 " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khgd.MaKHGD=nhp.MaKHGD and MaSV='" + MaSV + "'" +
252 " group by MaSV ";
253 SqlCommand cmd1 = new SqlCommand(sql1, con);
254 string tc = cmd1.ExecuteScalar().ToString();
255 // view diem10
256 string sql2 = " select 'cuoiky'=sum(DiemTk10*SoTC) " +
257 " from KetQua kq,NhomHocPhan nhp,HocPhan hp,KeHoachGiangDay khdt " +
258 " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khdt.MaKHGD=nhp.MaKHGD and MaSV='" + MaSV + "'" +
259 " group by MaSV ";
260 SqlCommand cmd2 = new SqlCommand(sql2, con);
261 string diem10 = cmd2.ExecuteScalar().ToString();
262 //view diem4
263 string sql3 = " select 'cuoiky'=sum(DiemTk4*SoTC) " +
264 " from KetQua kq,NhomHocPhan nhp,HocPhan hp,KeHoachGiangDay khdt " +
265 " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khdt.MaKHGD=nhp.MaKHGD and MaSV='" + MaSV + "'" +
266 " group by MaSV ";
267 SqlCommand cmd3 = new SqlCommand(sql3, con);
268 string diem4 = cmd3.ExecuteScalar().ToString();
269
270
271 string ketqua = "";
272 ketqua = Convert.ToDouble(diem10) / Convert.ToDouble(tc) + "," + Convert.ToDouble(diem4) / Convert.ToDouble(tc);
273 return ketqua;
274 }
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
289 public DataTable XemDiemLop(string MaHK,string MaLop)
290 {
291 SqlConnection con = new SqlConnection();
292 ChuanBi3Table(MaHK);
293 string sql4="select sv.MaSV,TenSV,TrungBinhCuoiKyHe10 "+
294 " from DTB left join SinhVien sv on DTB.MaSV=sv.MaSV "+
295 " where MaLop='"+MaLop+"'";
296 DataTable dt4 = da.GetTable(sql4);
297 da.ExcuteNonQuery("delete from Loptc where MaSV in ( select MaSV from Loptc )");
298 da.ExcuteNonQuery("delete from Lopdiem10 where MaSV in ( select MaSV from Lopdiem10 )");
299 da.ExcuteNonQuery("delete from DTB where MaSV in ( select MaSV from DTB )");
300 return dt4;
301 }
File name: ClassDAL.cs
Copy
12 public SqlConnection GetConnect()
13 {
14 return new SqlConnection("server=TY-PC;User id=sa;password=04091990;database=CNPM_1");
15 }
File name: ClassDAL.cs
Copy
16 public DataTable GetTable(string sql)
17 {
18 SqlConnection con = GetConnect();
19 SqlDataAdapter ad = new SqlDataAdapter(sql, con);
20 DataTable dt = new DataTable();
21 ad.Fill(dt);
22 return dt;
23 con.Close();
24 }
Download file with original file name:Connect
Connect 167 lượt xem
Gõ tìm kiếm nhanh...