Example #1
0
        /// <summary>
        /// read file input data to database
        /// </summary>
        /// <param name="path">file path</param>
        /// <returns>error information</returns>
        public static Err ReadTemplate(string path)
        {
            Err e = new Err();

            //data container
            string FileCon = "";

            //read file
            try
            {
                FileStream   fs = new FileStream(path, FileMode.OpenOrCreate);
                StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312"));
                FileCon = sr.ReadToEnd();
                sr.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                e.res  = false;
                e.info = ex.Message;
            }

            FileCon = FileCon.Replace("\r\n", "\n");

            string[] lines = FileCon.Split('\n');//in windows os using \r\n split each line

            string[] courseNames = lines[0].Split(',');

            List <string> courses = new List <string>();//import variable for store courses

            for (int a = 2; a < courseNames.Length; a++)
            {
                string coursename = courseNames[a]; //linq can't work with array (may be cause of index)
                courses.Add((from d in (new DataBase()).tbl_course where d.C_Name == coursename select d).Single().C_Num);
            }

            for (int a = 1; a < lines.Length; a++)
            {
                string[] cells = lines[a].Split(',');
                for (int c = 2; c < cells.Length; c++)
                {
                    using (var db = new DataBase())
                    {
                        tbl_stu_mark tsm = new tbl_stu_mark();
                        tsm.stu_num = cells[0];
                        tsm.cou_num = courses[c - 2];
                        tsm.mk_mark = Convert.ToDecimal(cells[c]);
                        db.tbl_stu_mark.AddObject(tsm);
                        db.SaveChanges();
                    }
                }
            }

            return(e);
        }
        public static Err mark_update(int index, decimal new_mark)
        {
            Err e = new Err();

            try
            {
                using (var db = new DataBase())
                {
                    tbl_stu_mark tsm = (
                        from d in (new DataBase()).tbl_stu_mark
                        where d.mk_index == index
                        select d
                        ).Single();
                    tsm.mk_mark = new_mark;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
            }
            return(e);
        }
        public static Err mark_update(v_mark vv)
        {
            Err e = new Err();

            try
            {
                using (var db = new DataBase())
                {
                    tbl_stu_mark tsm = (
                        from d in db.tbl_stu_mark
                        where d.mk_index == vv.mk_index
                        select d
                        ).Single();
                    tsm.mk_mark = vv.mk_mark;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                e.res  = false;
                e.info = ex.Message;
            }
            return(e);
        }