コード例 #1
0
        private void delete_records()
        {
            int RowsAffected = 0;

            cc.con = new SqlConnection(cs.DBConn);
            cc.con.Open();
            string ct = "delete from Student where C_ID=@d1";

            cc.cmd            = new SqlCommand(ct);
            cc.cmd.Connection = cc.con;
            cc.cmd.Parameters.AddWithValue("@d1", txtID.Text);
            RowsAffected = cc.cmd.ExecuteNonQuery();
            if (RowsAffected > 0)
            {
                st1 = lblUser.Text;
                st2 = "deleted the Student'" + label6 + "' having Student id '" + txtStudentID.Text + "'";
                cf.LogFunc(st1, System.DateTime.Now, st2);
                MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Reset();
            }
            else
            {
                MessageBox.Show("No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Reset();
            }
            if (cc.con.State == ConnectionState.Open)
            {
                cc.con.Close();
            }


            /* catch (Exception ex)
             * {
             *   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             * }*/
        }
コード例 #2
0
        //Update the data
        //---------------
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtStudentID.Text == "")
            {
                MessageBox.Show("Please enter student name", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtStudentID.Focus();
                return;
            }

            if (txtYear.Text == "")
            {
                MessageBox.Show("Please enter year", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtYear.Focus();
                return;
            }
            if (txtTerm.Text == "")
            {
                MessageBox.Show("Please enter term", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTerm.Focus();
                return;
            }
            if (txtPresentAbsent.Text == "")
            {
                MessageBox.Show("Please enter PresentAbsent.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPresentAbsent.Focus();
                return;
            }
            cc.con = new SqlConnection(cs.DBConn);
            cc.con.Open();
            string cb = "Update Student set StudentID=@d2,Name=@d3,Year=@d4,Term=@d5,Subject=@d6,PresentAbsent=@d7,Photo=@d8 where C_ID=@d1";

            cc.cmd            = new SqlCommand(cb);
            cc.cmd.Connection = cc.con;
            cc.cmd.Parameters.AddWithValue("@d1", txtID.Text);
            cc.cmd.Parameters.AddWithValue("@d2", txtStudentID.Text);
            cc.cmd.Parameters.AddWithValue("@d3", txtStudentName.Text);
            cc.cmd.Parameters.AddWithValue("@d4", txtYear.Text);
            cc.cmd.Parameters.AddWithValue("@d5", txtTerm.Text);
            cc.cmd.Parameters.AddWithValue("@d6", txtSubject.Text);
            cc.cmd.Parameters.AddWithValue("@d7", txtPresentAbsent.Text);
            MemoryStream ms       = new MemoryStream();
            Bitmap       bmpImage = new Bitmap(Picture.Image);

            bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[]       data = ms.GetBuffer();
            SqlParameter p    = new SqlParameter("@d8", SqlDbType.Image);

            p.Value = data;
            cc.cmd.Parameters.Add(p);
            cc.cmd.ExecuteReader();
            cc.con.Close();
            st1 = lblUser.Text;
            st2 = "updated the Student'" + txtStudentName.Text + "' having Student id '" + txtStudentID.Text + "'";
            cf.LogFunc(st1, System.DateTime.Now, st2);
            btnUpdate.Enabled = false;
            MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);

            /*
             * catch (Exception ex)
             * {
             *   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             * }*/
        }