Ejemplo n.º 1
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //선택된 학생을 삭제할건지 확인
            int    rowIndex = dataGridView1.CurrentRow.Index;
            string name     = dataGridView1[1, rowIndex].Value.ToString();

            if (MessageBox.Show($"{name} 학생 정보를 삭제하시겠습니까?", "삭제 확인", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //DB에서 데이터 삭제

                int       stuID  = (int)dataGridView1[0, dataGridView1.CurrentRow.Index].Value;
                StudentDB db     = new StudentDB();
                bool      result = db.Delete(stuID);
                db.Dispose();

                if (result)
                {
                    //MessageBox.Show("삭제되었습니다.");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("다시 시도하여 주십시오.");
                }
            }
        }
Ejemplo n.º 2
0
        private void btnUpdate_Click(object sender, EventArgs e)//수정
        {
            //현재 그리드뷰에서 선택된 학생 정보를 조회
            int rowIndex = dataGridView1.CurrentRow.Index;

            Student stu;

            stu.ID   = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
            stu.Name = dataGridView1[1, rowIndex].Value.ToString(); //위와 같지만 다른 방법
            stu.Dept = dataGridView1[2, rowIndex].Value.ToString();

            //학생 정보를 수정폼에 전달해서 오픈
            frmStudentInsUp frm = new frmStudentInsUp(frmStudentInsUp.OpenMode.Update);

            frm.stuInfo = stu;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                //변경된 학생 정보를 DB에 수정
                StudentDB db     = new StudentDB();
                bool      result = db.Update(frm.stuInfo);
                db.Dispose();
                if (result)
                {
                    MessageBox.Show("수정 되었습니다.");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("다시 시도하여 주십시오.");
                }
            }
        }
Ejemplo n.º 3
0
        private void LoadData()
        {
            StudentDB db = new StudentDB();
            DataTable dt = db.GetAllData();

            db.Dispose();
            dataGridView1.DataSource = dt;
        }
Ejemplo n.º 4
0
        private void LoadData()
        {
            StudentDB db = new StudentDB();
            DataTable dt = db.GetAllData();

            db.Dispose();
            dataGridView1.DataSource = dt; //DataSet, DataTable, DataView, List<T> 등등 가능
            //dataGridView1.ClearSelection(); 로드시 파란색줄 선택 없게하는
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 도서예약
        /// </summary>
        /// <param name="stuid">학번</param>
        /// <param name="bookid">도서번호</param>
        /// <returns>예약성공여부</returns>
        public bool ReserveBook(int stuid, int bookid)
        {
            //유효성체크
            //학번이 유효한지, 도서번호가 유효한지, 대여가능한지, 예약상태인지
            //대여한 학생이 또 예약하는 것은 현재 허용
            StudentDB stu = new StudentDB();

            if (!stu.IsValid(stuid))
            {
                throw new Exception("유효한 학번이 아닙니다.");
            }
            stu.Dispose();

            BookDB bk = new BookDB();

            if (!bk.IsValid(bookid))
            {
                throw new Exception("유효한 도서가 아닙니다.");
            }
            else if (!bk.IsLended(bookid))
            {
                throw new Exception("대여 가능한 도서입니다.");
            }
            else
            {
                if (bk.IsReserved(bookid))
                {
                    throw new Exception("이미 예약된 도서입니다.");
                }
                bk.Dispose();

                try
                {
                    MySqlCommand cmd = new MySqlCommand();
                    cmd.CommandText = "update book set reservestuid = @reservestuid where bookid = @bookid";
                    cmd.Connection  = conn;

                    cmd.Parameters.Add("@reservestuid", MySqlDbType.Int32);
                    cmd.Parameters["@reservestuid"].Value = stuid;

                    cmd.Parameters.Add("@bookid", MySqlDbType.Int32);
                    cmd.Parameters["@bookid"].Value = bookid;

                    cmd.ExecuteNonQuery();
                    return(true);
                }
                catch (Exception err)
                {
                    throw err;
                    //return false; //도달하지 않는 코드
                }
            }
        }
Ejemplo n.º 6
0
        private void button2_Click(object sender, EventArgs e)
        {
            frmStudentInsUp frm = new frmStudentInsUp(frmStudentInsUp.OpenMode.Insert);

            if (frm.ShowDialog() == DialogResult.OK)
            {
                //입력받은 값으로 DB에 저장
                Student   stu    = frm.stuInfo;
                StudentDB db     = new StudentDB();
                bool      result = db.Insert(stu);
                db.Dispose();
                if (result)
                {
                    MessageBox.Show("추가 되었습니다.");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("다시 시도하여 주십시오.");
                }
            }
        }
Ejemplo n.º 7
0
        public void LendBook(int studentID, int[] bookIDs)
        {
            StudentDB student = new StudentDB();
            bool      result  = student.IsValid(studentID);

            if (!result)
            {
                throw new Exception("존재하지 않는 학생입니다.");
            }
            student.Dispose();

            //대여 로직 시작

            // 1. Lending Insert 1건

            // 2. LendingItem Insert 여러 건
            //    Book Update 여러건(lendingState, reserveStuID)

            //예약한 학번이 대여학번과 동ㅇ리하면 예약학번을 Clear
            //update, delete는 where절이 참일때만 dml문장이 실행된다.
            //0건 적용이 되더라도 오류발생X

            //트랜잭션 : 여러개의 커맨드를 하나의 단위로 묶어서 처리
            //          여러개의 커맨드가 모두 성공이면 마지막에 commit(), 문제시 실행되었던 모든 커맨드를 rollback()
            MySqlTransaction trans = conn.BeginTransaction();

            try
            {
                MySqlCommand cmd          = new MySqlCommand();
                MySqlCommand updateCmd    = new MySqlCommand();
                MySqlCommand updateRevCmd = new MySqlCommand();
                //cmd.CommandText = "SP_StudentInsUp";
                //cmd.CommandType = CommandType.StoredProcedure;
                //cmd.CommandType = CommandType.Text;

                cmd.CommandText = $"insert into lending(studentid, lenddate) values (@studentid, now());select last_insert_id();";
                //-- 마스터 / 디테일의 관계로 Insert할 때에는 마스터를 insert하면서 동시에 AI된 값을 Select해아만 한다.
                cmd.Connection  = conn;
                cmd.Transaction = trans;

                //MySqlParameter param = new MySqlParameter("@studentid", MySqlDbType.Int32);
                //param.Value = studentID;
                //cmd.Parameters.Add(param);

                cmd.Parameters.Add("@studentid", MySqlDbType.Int32);
                cmd.Parameters["@studentid"].Value = studentID;

                //cmd.Parameters.AddWithValue("@studentid", studentID);
                // AddWithValue() 값에 따라서 자동으로 타입 결정

                int newLendingID = Convert.ToInt32(cmd.ExecuteScalar()); //신규 입력된 AI값

                //-------------------------------------------------------------------------
                cmd.Parameters.Clear();
                cmd.CommandText = $@"insert into lendingitem (lendingid, bookitem, bookid) 
                                         values(@lendingid, @bookitem, @bookid)";
                cmd.Parameters.Add("@lendingid", MySqlDbType.Int32);
                cmd.Parameters.Add("@bookitem", MySqlDbType.Int32);
                cmd.Parameters.Add("@bookid", MySqlDbType.Int32);

                updateCmd.Connection  = conn;
                updateCmd.Transaction = trans;
                updateCmd.CommandText = $"update book set lendingstate = 1 where bookid = @bookid";
                updateCmd.Parameters.Add("@bookid", MySqlDbType.Int32);

                updateRevCmd.Connection  = conn;
                updateRevCmd.Transaction = trans;
                updateRevCmd.CommandText = $"update book set reservestuid = 0 where bookid = @bookid and reservestuid = @reservestuid";
                updateRevCmd.Parameters.Add("@bookid", MySqlDbType.Int32);
                updateRevCmd.Parameters.Add("@reservestuid", MySqlDbType.Int32);

                for (int i = 0; i < bookIDs.Length; i++)
                {
                    cmd.Parameters["@lendingid"].Value = newLendingID;
                    cmd.Parameters["@bookitem"].Value  = i + 1;
                    cmd.Parameters["@bookid"].Value    = bookIDs[i];
                    cmd.ExecuteNonQuery();

                    //---------------------------------------------------------------------------

                    updateCmd.Parameters["@bookid"].Value = bookIDs[i];
                    updateCmd.ExecuteNonQuery();

                    //---------------------------------------------------------------------------

                    updateRevCmd.Parameters["@bookid"].Value       = bookIDs[i];
                    updateRevCmd.Parameters["@reservestuid"].Value = studentID;
                    updateRevCmd.ExecuteNonQuery();
                }

                trans.Commit();
            }
            catch (Exception err)
            {
                trans.Rollback();
                throw err;
            }
        }