Visual studio

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {        

        public Form1()

        {

            InitializeComponent();           

        }

        #region "Khai báo biến sử dụng cho việc truy cập cơ sở dữ liệu"

        //đối tượng kết nối

        private SqlConnection cn;

        // đối tượng thực hiện lệnh

        private SqlCommand cmd;

        //đối tượng lọc dữ liệu

        private SqlDataAdapter da;

        //đối tượng chứa dữ liệu tạm thời.

        private DataSet ds;

        #endregion

        #region"viết hàm hiển thị câu hỏi "

        private void Hienthicauhoi()

        {

            //b1: thực hiện mở kết nối

            cn = new SqlConnection(@"Data Source=A42F-PC\SQLEXPRESS;Initial Catalog=Trondethi;User ID=sa;Password=1122");

            cn.Open();

            //b2: thực hiện lệnh theo yêu cầu: (hiển thị <xuất dữ liệu>)

            //khởi tạo đối tượng command thông qua phương thức CreateCommand() của SqlConnection.

            cmd = cn.CreateCommand();

            //th1:

            //cmd.CommandType=CommandType.Text;

            //cmd.CommandText="Select * from Cauhoi";

            //th2:

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "HienthiCauhoi";

            //b3: đổ dữ liệu ra dataset thông qua cầu nối DataAdapter

            da = new SqlDataAdapter(cmd);

            ds = new DataSet("bảng chứa dữ liệu");

            //sử dụng phương thức Fill() của dataAdapter

            da.Fill(ds);

            //b4: Đóng kết nối lại

            cn.Close();

            this.dataGridView1.DataSource = ds.Tables[0];

        }

        #endregion

        #region"viết hàm hiển thị câu hỏi theo mã câu hỏi "

        private void Hienthicauhoitheoma(int macauhoi)

        {

            //b1: thực hiện mở kết nối

            cn = new SqlConnection(@"Data Source=A42F-PC\SQLEXPRESS;Initial Catalog=Trondethi;User ID=sa;Password=1122");

            cn.Open();

            //b2: thực hiện lệnh theo yêu cầu: (hiển thị <xuất dữ liệu>)

            //khởi tạo đối tượng command thông qua phương thức CreateCommand() của SqlConnection.

            cmd = cn.CreateCommand();

            //th1:

            //cmd.CommandType=CommandType.Text;

            //cmd.CommandText="Select * from Cauhoi";

            //th2:

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "HienthiCauhoitheoMa";

            //khởi tạo các tham số truyền dữ liệu vào.

            SqlParameter pMacauhoi = new SqlParameter("@Macauhoi", SqlDbType.Int);

            pMacauhoi.Value = macauhoi;

            cmd.Parameters.Add(pMacauhoi);           

            //b3: đổ dữ liệu ra dataset thông qua cầu nối DataAdapter

            da = new SqlDataAdapter(cmd);

            ds = new DataSet("bảng chứa dữ liệu");

            //sử dụng phương thức Fill() của dataAdapter

            da.Fill(ds);

            //b4: Đóng kết nối lại

            cn.Close();

            //đổ dữ liệu ra các ô textbox

            txtMacauhoi.Text = ds.Tables[0].Rows[0]["Macauhoi"].ToString();

            txtnoidungcauhoi.Text = ds.Tables[0].Rows[0]["Noidungcauhoi"].ToString();

            txtnoidungdapan.Text = ds.Tables[0].Rows[0]["Noidungdapan"].ToString();

            cbmucdo.SelectedValue = ds.Tables[0].Rows[0]["Mamucdo"].ToString();

            cbHinhthuc.SelectedValue = ds.Tables[0].Rows[0]["Mahinhthuc"].ToString();

            cbNhomkienthuc.SelectedValue = ds.Tables[0].Rows[0]["Manhomkienthuc"].ToString();

        }

        #endregion

        #region"viết hàm hiển thị câu hỏi theo mã nhóm kiến thức "

        private void Hienthicauhoitheomanhomkienthuc(int manhomkienthuc)

        {

            //b1: thực hiện mở kết nối

            cn = new SqlConnection(@"Data Source=A42F-PC\SQLEXPRESS;Initial Catalog=Trondethi;User ID=sa;Password=1122");

            cn.Open();

            //b2: thực hiện lệnh theo yêu cầu: (hiển thị <xuất dữ liệu>)

            //khởi tạo đối tượng command thông qua phương thức CreateCommand() của SqlConnection.

            cmd = cn.CreateCommand();

            //th1:

            //cmd.CommandType=CommandType.Text;

            //cmd.CommandText="Select * from Cauhoi";

            //th2:

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "HienthiCauhoitheoNhomkienthuc";

            //khởi tạo các tham số truyền dữ liệu vào.

            SqlParameter pMacauhoi = new SqlParameter("@MaNhomkienthuc", SqlDbType.Int);

            pMacauhoi.Value = manhomkienthuc;

            cmd.Parameters.Add(pMacauhoi);

            //b3: đổ dữ liệu ra dataset thông qua cầu nối DataAdapter

            da = new SqlDataAdapter(cmd);

            ds = new DataSet("bảng chứa dữ liệu");

            //sử dụng phương thức Fill() của dataAdapter

            da.Fill(ds);

            //b4: Đóng kết nối lại

            cn.Close();

            //đổ dữ liệu ra các ô textbox

            this.dataGridView1.DataSource = ds.Tables[0];

        }

        #endregion

        #region"viết hàm thêm mới câu hỏi "

        private void Themmoicauhoi()

        {

            //b1: thực hiện mở kết nối

            cn = new SqlConnection(@"Data Source=A42F-PC\SQLEXPRESS;Initial Catalog=Trondethi;User ID=sa;Password=1122");

            cn.Open();

            //b2: thực hiện lệnh theo yêu cầu: (Thêm mới <nhập dữ liệu>)

            //khởi tạo đối tượng command thông qua phương thức CreateCommand() của SqlConnection.

            cmd = cn.CreateCommand();

            //th1:

            //cmd.CommandType=CommandType.Text;

            //cmd.CommandText="Select * from Cauhoi";

            //th2:

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "Themmoicauhoi";

            //b3: Đưa dữ liệu vào bên trong csdl.

            //khởi tạo các tham số truyền dữ liệu vào.

            SqlParameter pNoidungcauhoi = new SqlParameter("@Noidungcauhoi",SqlDbType.NVarChar);

            pNoidungcauhoi.Value = txtnoidungcauhoi.Text;

            cmd.Parameters.Add(pNoidungcauhoi);            

            SqlParameter pNoidungdapap = new SqlParameter("@Noidungdapap", SqlDbType.NVarChar);

            pNoidungdapap.Value = txtnoidungdapan.Text;

            cmd.Parameters.Add(pNoidungdapap);

            SqlParameter pMamucdo = new SqlParameter("@Mamucdo", SqlDbType.Int);

            pMamucdo.Value = int.Parse(cbmucdo.SelectedValue.ToString());

            cmd.Parameters.Add(pMamucdo);

            SqlParameter pMahinhthuc = new SqlParameter("@Mahinhthuc", SqlDbType.Int);

            pMahinhthuc.Value = int.Parse(cbHinhthuc.SelectedValue.ToString());

            cmd.Parameters.Add(pMahinhthuc);

            SqlParameter pManhomkienthuc = new SqlParameter("@Manhomkienthuc", SqlDbType.Int);

            pManhomkienthuc.Value = int.Parse(cbNhomkienthuc.SelectedValue.ToString()); ;

            cmd.Parameters.Add(pManhomkienthuc);

            //phương thức thực hiện lệnh thêm mới dữ liệu 

            int kq = 0;

            kq=cmd.ExecuteNonQuery();

            if (kq==1)

            {

                MessageBox.Show("Thêm mới thành công");

            }

            else

            {

                MessageBox.Show("Thêm mới thất bại");

            }

            //b4: Đóng kết nối lại

            cn.Close();

        }

        #endregion

        #region"viết hàm cập nhật câu hỏi "

        private void Capnhatcauhoi()

        {

            //b1: thực hiện mở kết nối

            cn = new SqlConnection(@"Data Source=A42F-PC\SQLEXPRESS;Initial Catalog=Trondethi;User ID=sa;Password=1122");

            cn.Open();

            //b2: thực hiện lệnh theo yêu cầu: (Thêm mới <nhập dữ liệu>)

            //khởi tạo đối tượng command thông qua phương thức CreateCommand() của SqlConnection.

            cmd = cn.CreateCommand();

            //th1:

            //cmd.CommandType=CommandType.Text;

            //cmd.CommandText="Select * from Cauhoi";

            //th2:

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "Suacauhoi";

            //b3: Đưa dữ liệu vào bên trong csdl.

            //khởi tạo các tham số truyền dữ liệu vào.

            SqlParameter pMacauhoi = new SqlParameter("@Macauhoi", SqlDbType.Int);

            pMacauhoi.Value =int.Parse(txtMacauhoi.Text);

            cmd.Parameters.Add(pMacauhoi);

            SqlParameter pNoidungcauhoi = new SqlParameter("@Noidungcauhoi", SqlDbType.NVarChar);

            pNoidungcauhoi.Value = txtnoidungcauhoi.Text;

            cmd.Parameters.Add(pNoidungcauhoi);

            SqlParameter pNoidungdapap = new SqlParameter("@Noidungdapap", SqlDbType.NVarChar);

            pNoidungdapap.Value = txtnoidungdapan.Text;

            cmd.Parameters.Add(pNoidungdapap);

            SqlParameter pMamucdo = new SqlParameter("@Mamucdo", SqlDbType.Int);

            pMamucdo.Value = int.Parse(cbmucdo.SelectedValue.ToString());

            cmd.Parameters.Add(pMamucdo);

            SqlParameter pMahinhthuc = new SqlParameter("@Mahinhthuc", SqlDbType.Int);

            pMahinhthuc.Value = int.Parse(cbHinhthuc.SelectedValue.ToString());

            cmd.Parameters.Add(pMahinhthuc);

            SqlParameter pManhomkienthuc = new SqlParameter("@Manhomkienthuc", SqlDbType.Int);

            pManhomkienthuc.Value = int.Parse(cbNhomkienthuc.SelectedValue.ToString()); ;

            cmd.Parameters.Add(pManhomkienthuc);

            //phương thức thực hiện lệnh thêm mới dữ liệu 

            int kq = 0;

            kq = cmd.ExecuteNonQuery();

            if (kq == 1)

            {

                MessageBox.Show("Cập nhật thành công");

            }

            else

            {

                MessageBox.Show("Cập nhật thất bại");

            }

            //b4: Đóng kết nối lại

            cn.Close();

        }

        #endregion

        #region"viết hàm xóa câu hỏi "

        private void Xoacauhoi()

        {

            //b1: thực hiện mở kết nối

            cn = new SqlConnection(@"Data Source=A42F-PC\SQLEXPRESS;Initial Catalog=Trondethi;User ID=sa;Password=1122");

            cn.Open();

            //b2: thực hiện lệnh theo yêu cầu: (Thêm mới <nhập dữ liệu>)

            //khởi tạo đối tượng command thông qua phương thức CreateCommand() của SqlConnection.

            cmd = cn.CreateCommand();

            //th1:

            //cmd.CommandType=CommandType.Text;

            //cmd.CommandText="Select * from Cauhoi";

            //th2:

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "Xoacauhoi";

            //b3: Đưa dữ liệu vào bên trong csdl.

            //khởi tạo các tham số truyền dữ liệu vào.

            SqlParameter pMacauhoi = new SqlParameter("@Macauhoi", SqlDbType.Int);

            pMacauhoi.Value = int.Parse(txtMacauhoi.Text);

            cmd.Parameters.Add(pMacauhoi);           

            //phương thức thực hiện lệnh thêm mới dữ liệu 

            int kq = 0;

            kq = cmd.ExecuteNonQuery();

            if (kq == 1)

            {

                MessageBox.Show("Xóa thành công");

            }

            else

            {

                MessageBox.Show("Xóa thất bại");

            }

            //b4: Đóng kết nối lại

            cn.Close();

        }

        #endregion

        #region"phương thức Load"

        private void Form1_Load(object sender, EventArgs e)

        {

            // TODO: This line of code loads data into the 'trondethiDataSet3.Nhomkienthuc' table. You can move, or remove it, as needed.

            this.nhomkienthucTableAdapter.Fill(this.trondethiDataSet3.Nhomkienthuc);

            // TODO: This line of code loads data into the 'trondethiDataSet3.Hinhthuc' table. You can move, or remove it, as needed.

            this.hinhthucTableAdapter.Fill(this.trondethiDataSet3.Hinhthuc);

            // TODO: This line of code loads data into the 'trondethiDataSet3.Mucdo' table. You can move, or remove it, as needed.

            this.mucdoTableAdapter.Fill(this.trondethiDataSet3.Mucdo);

            // TODO: This line of code loads data into the 'trondethiDataSet3.Nhomkienthuc' table. You can move, or remove it, as needed.

            Hienthicauhoi();

        }

        #endregion

        private void button2_Click(object sender, EventArgs e)

        {

            if (txtMacauhoi.Text=="")

            {

                // thêm mới câu hỏi

                Themmoicauhoi();

            }

            else

            {

                // cập nhật câu hỏi

                Capnhatcauhoi();

            }           

            Hienthicauhoi();

            Xoadulieu();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {

           int macauhoi=int.Parse(dataGridView1.CurrentRow.Cells["Macauhoi"].Value.ToString());

           Hienthicauhoitheoma(macauhoi);

            DialogResult kq;

            switch (e.ColumnIndex)

            {

                case 0: // sửa dữ liệu

                    //Capnhatcauhoi(); 

                    break;

                case 1: // xóa dữ liệu

                    kq=MessageBox.Show("Bạn có chắc chắn không ?", "Thông báo", MessageBoxButtons.YesNo);

                    if (kq==DialogResult.Yes)

                    {

                        Xoacauhoi();

                    }

                    break;

            }

            Hienthicauhoi();

        }

        private void button3_Click(object sender, EventArgs e)

        {

            Xoadulieu();

        }

        private void Xoadulieu()

        {

            txtMacauhoi.Text = "";

            txtnoidungcauhoi.Text = "";

            txtnoidungdapan.Text="";

            cbHinhthuc.SelectedValue="0";

            cbmucdo.SelectedValue="0";

            cbNhomkienthuc.SelectedValue="0";

        }

        private void button1_Click(object sender, EventArgs e)

        {

            int manhomkienthuc = int.Parse(cblocnhomkienthuc.SelectedValue.ToString());

            Hienthicauhoitheomanhomkienthuc(manhomkienthuc);

        }

        private void button4_Click(object sender, EventArgs e)

        {

            Hienthicauhoi();

        }

    }

}

Bạn đang đọc truyện trên: truyentop.pro

Tags: