Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         public string TinhDiemTB(string MaSV)
         {
             SqlConnection con = da.GetConnect();
             con.Open();
             //view tc
             string sql1 = " select 'sotinchi'=sum(SoTC) " +
                                " from KetQua kq,HocPhan hp,NhomHocPhan nhp,KeHoachGiangDay khgd " +
                                " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khgd.MaKHGD=nhp.MaKHGD and MaSV='" + MaSV + "'" +
                                " group by MaSV ";
             SqlCommand cmd1 = new SqlCommand(sql1, con);
             string tc = cmd1.ExecuteScalar().ToString();
             // view diem10
             string sql2 = " select 'cuoiky'=sum(DiemTk10*SoTC) " +
                                " from KetQua kq,NhomHocPhan nhp,HocPhan hp,KeHoachGiangDay khdt " +
                                " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khdt.MaKHGD=nhp.MaKHGD and MaSV='" + MaSV + "'" +
                                " group by MaSV ";
             SqlCommand cmd2 = new SqlCommand(sql2, con);
             string diem10 = cmd2.ExecuteScalar().ToString();
             //view diem4
             string sql3 = " select 'cuoiky'=sum(DiemTk4*SoTC) " +
                                " from KetQua kq,NhomHocPhan nhp,HocPhan hp,KeHoachGiangDay khdt " +
                                " where kq.MaNhomHP=nhp.MaNhomHP and nhp.MaHP=hp.MaHP and khdt.MaKHGD=nhp.MaKHGD and MaSV='" + MaSV + "'" +
                                " group by MaSV ";
             SqlCommand cmd3 = new SqlCommand(sql3, con);
             string diem4 = cmd3.ExecuteScalar().ToString();


             string ketqua = "";
             ketqua = Convert.ToDouble(diem10) / Convert.ToDouble(tc) + "," + Convert.ToDouble(diem4) / Convert.ToDouble(tc);
             return ketqua;
         }