Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

baithi

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.OleDb;

namespace BaiThi.DungChung

{

    class thaotacdb

    {

        public static OleDbConnection ketnoi()

        {

            return new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\DataKTHT.mdb");

        }

        public static void ThucHien(string SQL)

        {

            OleDbConnection conn = ketnoi();

            conn.Open();

            OleDbCommand cmd = new OleDbCommand(SQL, conn);

            cmd.ExecuteNonQuery();

            conn.Close();

        }

        public static DataSet LayDataset(string SQL)

        {

            OleDbConnection conn = ketnoi();

            conn.Open();

            OleDbDataAdapter ad = new OleDbDataAdapter(SQL, conn);

            DataSet ds = new DataSet();

            ad.Fill(ds);

            return ds;

        }

    }

}

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using BaiThi.DungChung;

namespace BaiThi.Thaotacbang

{

    class tblLop

    {

        string maLop, tenLop, khoa;

        int namvao;

        public string MaLop

        {

            set { maLop = value; }

            get { return maLop; }

        }

           public string TenLop

        {

            set { tenLop = value; }

            get {return tenLop;  }

        }

           public string Khoa

        {

            set { khoa = value; }

            get {return khoa ; }

        }

           public int Namvao

        {

            set { namvao = value; }

            get { return namvao; }

        }

        public void Them()

        {

            string sql = string.Format("insert into Lop values('{0}','{1}',{2},'{3}')", MaLop, TenLop, Namvao, Khoa);

            thaotacdb.ThucHien(sql);

        }

        public void sua()

        {

            string sql = string.Format("update Lop set TenLop ='{0}',namvao = {1},Khoa='{2}' where MaLop = '{3}'", TenLop, Namvao, Khoa, MaLop);

            thaotacdb.ThucHien(sql);

        }

        public static void xoa(string malop)

        {

            string sql = string .Format ("delete from lop where malop = '{0}'",malop );

            thaotacdb.ThucHien(sql);

        }

        public static List<tblLop> LayDSLop(string DK)

        {

            string sql = string.Format("select * from lop where {0}", DK);

            DataSet ds = thaotacdb.LayDataset(sql);

            List<tblLop> lst = new List<tblLop>();

            foreach (DataRow  r in ds.Tables [0].Rows )

            {

                tblLop t = new tblLop();

                t.MaLop = r["MaLop"].ToString();

                t.TenLop = r["TenLop"].ToString();

                t.Namvao =int.Parse ( r["Namvao"].ToString());

                t.Khoa = r["Khoa"].ToString();

                lst.Add(t);

            }

            return lst;

        }

        public static tblLop LayTTLop(string MaLop)

        {

            string sql = string.Format("select * from Lop where MaLop ='{0}'", MaLop);

            DataSet ds = thaotacdb.LayDataset(sql);

            if (ds.Tables[0].Rows.Count >0)

            {

                tblLop t = new tblLop();

                t.MaLop = ds.Tables[0].Rows[0]["MaLop"].ToString();

                t.TenLop = ds.Tables[0].Rows[0]["TenLop"].ToString();

                t.Namvao = int.Parse(ds.Tables[0].Rows[0]["Namvao"].ToString());

                t.Khoa = ds.Tables[0].Rows[0]["Khoa"].ToString();

                return t;

            }

            else

                return null;

        }

    }

}

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using BaiThi.Thaotacbang;

namespace BaiThi

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {         

            List<tblLop> lst = tblLop.LayDSLop("1=1");

            gridLop.DataSource = lst;

        }

        private void Them_Click(object sender, EventArgs e)

        {

            double tmp;

            if (txtMaLop.Text == "" || txtTenLop.Text == "" || txtKhoa.Text == "" || double.TryParse(txtNamVao.Text, out  tmp) == false)

            {

                MessageBox.Show("Du lieu nhap vao ko dung");

                return;

            }

            if (tblLop.LayTTLop(txtMaLop .Text ) != null  )

            {

                MessageBox.Show("Du lieu TRUNG");

                return;

            }

            tblLop t = new tblLop();

            t.MaLop = txtMaLop.Text;

            t.TenLop = txtTenLop.Text;

            t.Namvao = int.Parse(txtNamVao.Text);

            t.Khoa = txtKhoa.Text;

            t.Them();

            MessageBox.Show("Da Them");

            List<tblLop> lst = tblLop.LayDSLop("1=1");

            gridLop.DataSource = lst;

        }

        private void gridLop_Click(object sender, EventArgs e)

        {

            string MaLop = gridLop.CurrentRow.Cells["MaLop"].Value.ToString();

            tblLop t = tblLop.LayTTLop(MaLop);

            txtMaLop.Text = t.MaLop;

            txtTenLop.Text = t.TenLop;

            txtNamVao.Text = t.Namvao.ToString();

            txtKhoa .Text = t.Khoa ;

        }

        private void Sua_Click(object sender, EventArgs e)

        {

            tblLop t = new tblLop();

            t.MaLop = txtMaLop.Text;

            t.TenLop = txtTenLop.Text;

            t.Namvao = int.Parse(txtNamVao.Text);

            t.Khoa = txtKhoa.Text;

            t.sua();

            MessageBox.Show("Da sua");

            List<tblLop> lst = tblLop.LayDSLop("1=1");

            gridLop.DataSource = lst;

        }

        private void Xoa_Click(object sender, EventArgs e)

        {

            string malop = gridLop.CurrentRow.Cells["MaLop"].Value.ToString();

            tblLop.xoa(malop);

            MessageBox.Show("Da Xoa");

            List<tblLop> lst = tblLop.LayDSLop("1=1");

            gridLop.DataSource = lst;

        }

        private void thoat_Click(object sender, EventArgs e)

        {

            Close();

        }

        private void nhaplai_Click(object sender, EventArgs e)

        {

            txtMaLop.Text = "";

            txtTenLop.Text = "";

            txtNamVao.Text = "";

            txtKhoa.Text = "";

        }

    }

}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: #123