them sua xoa C#
using System;
using System.Collections.Generic;
using System.Text;
using System .Data ;
using System .Data .OleDb;
namespace MONHOC
{
class MH
{
public OleDbConnection taoketnoi()
{
return new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DataKTHT.mdb");
}
public DataTable taobang(string sql)
{
OleDbConnection con = taoketnoi();
OleDbDataAdapter ad = new OleDbDataAdapter(sql, con);
DataTable dt = new DataTable ();
ad.Fill(dt);
return dt;
}
public void ExcuteNonQuery(string sql)
{
OleDbConnection con = taoketnoi();
OleDbCommand cmd = new OleDbCommand(sql,con );
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MONHOC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
MH m = new MH();
private void button2_Click(object sender, EventArgs e)
{
string sql = "select * from MON_HOC ";
dataGridView1.DataSource = m.taobang(sql);
}
private void button1_Click(object sender, EventArgs e)
{
string sql = "insert into MON_HOC(MaMon,TenMon,SoDVHT) values('" + textBox1.Text + "','" + textBox2.Text + "','"+ textBox3.Text +"') ";
try
{
m.ExcuteNonQuery(sql);
}
catch
{
MessageBox.Show("Da ton tai sinh vien nay", "Loi");
}
string sql1 = "select * from MON_HOC ";
dataGridView1.DataSource = m.taobang(sql1);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
int dong;
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dong = e.RowIndex;
textBox1.Text = dataGridView1.Rows[dong].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[dong].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[dong].Cells[2].Value.ToString();
}
private void button4_Click(object sender, EventArgs e)
{
string sql = "update MON_HOC set TenMon ='" + textBox2.Text + "',SoDVHT= '" + textBox3.Text + "' where MaMon = '"+textBox1.Text +"' ";
m.ExcuteNonQuery(sql);
string sql1 = "select * from MON_HOC ";
dataGridView1.DataSource = m.taobang(sql1);
}
private void button3_Click(object sender, EventArgs e)
{
string sql = "delete from Mon_hoc where MaMon = '" + textBox1.Text + "'";
m.ExcuteNonQuery(sql);
string sql1 = "select * from MON_HOC ";
dataGridView1.DataSource = m.taobang(sql1);
}
}
}
class ThaoTacDB <Dung Chung
{
//public static string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\QLBH.mdb";
public static OleDbConnection ketnoi()
{
return new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\QLBH.mdb");
}
public static void ExcuteNonQuery(string sql)
{
//connStr;
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;
}
}
class tblHang < Thao tac bang>
{
string maHang, tenHang;
double giaBan, giaNhap;
public string MaHang
{
set { maHang = value; }
get { return maHang; }
}
public string TenHang
{
set { tenHang = value; }
get { return tenHang; }
}
public Double GiaBan
{
set { giaBan = value; }
get { return giaBan; }
}
public Double GiaNhap
{
set { giaNhap = value; }
get { return giaNhap; }
}
public void AddNew()
{
string sql = string .Format ("insert into Hang values('{0}','{1}',{2},{3})",MaHang ,TenHang ,GiaBan ,GiaNhap);
ThaoTacDB.ExcuteNonQuery(sql);
}
public void Update()
{
string sql = string.Format("update Hang set TenHang = '{0}',GiaBan={1},GiaNhap = {2} where MaHang = '{3}'", TenHang, GiaBan, GiaNhap, MaHang);
ThaoTacDB.ExcuteNonQuery(sql);
}
public static void Delete(string MaHang)
{
string sql = string.Format("delete from Hang where MaHang = '{0}'", MaHang);
ThaoTacDB.ExcuteNonQuery(sql);
}
public static tblHang LayttHang(string MaHang)
{
string sql = string.Format("select * from Hang where MaHang='{0}'",MaHang);
DataSet ds = ThaoTacDB.LayDataset(sql);
if (ds.Tables[0].Rows.Count > 0)
{
tblHang h = new tblHang();
h.MaHang = ds.Tables[0].Rows[0]["MaHang"].ToString();
h.TenHang = ds.Tables[0].Rows[0]["TenHang"].ToString();
h.GiaBan = double.Parse(ds.Tables[0].Rows[0]["GiaBan"].ToString());
h.GiaNhap = double.Parse(ds.Tables[0].Rows[0]["GiaNhap"].ToString());
return h;
}
else
return null;
}
public static List<tblHang> LaydsHang( string dk)
{
string sql = string.Format("select * from Hang where '{0}'", dk);
DataSet ds = ThaoTacDB.LayDataset(sql);
List<tblHang> lst = new List<tblHang>();
foreach (DataRow r in ds.Tables[0].Rows)
{
tblHang tg = new tblHang();
tg.MaHang = r["MaHang"].ToString();
tg.TenHang = r["TenHang"].ToString();
tg.GiaBan =double .Parse ( r["GiaBan"].ToString());
tg.GiaNhap = double.Parse(r["GiaNhap"].ToString());
lst.Add(tg);
}
return lst;
}
}
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Delete_Load(object sender, EventArgs e)
{
string dk = "1=1";
List<tblHang> lst = tblHang.LaydsHang(dk );
dataGridView1.DataSource = lst;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
tblHang h = new tblHang();
string MaHang;
MaHang = dataGridView1.CurrentRow.Cells["MaHang"].Value.ToString();
tblHang.Delete(MaHang);
MessageBox.Show("Xoá Thành Công !");
}
}
public partial class Them : Form
{
public Them()
{
InitializeComponent();
}
private void butThem_Click(object sender, EventArgs e)
{
double tmp;
if (txtMaHang.Text.Trim () == "" || txtTenHang.Text.Trim () == "" || double.TryParse(txtGiaBan.Text, out tmp) == false || double.TryParse(txtGiaNhap.Text, out tmp) == false)
{
MessageBox.Show("Dữ Liệu Không Hợp Lệ !", "Thông Báo");
return;
}
tblHang h;
h = tblHang.LayttHang(txtMaHang.Text);
if (h != null)
{
MessageBox.Show("Trùng Mã !", "Thông Báo");
return;
}
else
{
h = new tblHang();
h.TenHang = txtTenHang.Text;
h.MaHang = txtMaHang.Text;
h.GiaBan = double .Parse (txtGiaBan.Text.ToString ());
h.GiaNhap = double.Parse(txtGiaNhap.Text.ToString());
h.AddNew();
MessageBox.Show("Thêm Mới Thành Công !", "Thông Báo");
}
}
private void button1_Click(object sender, EventArgs e)
{
tblHang h = new tblHang();
h.TenHang = txtTenHang.Text;
h.MaHang = txtMaHang.Text;
h.GiaBan = double.Parse(txtGiaBan.Text.ToString());
h.GiaNhap = double.Parse(txtGiaNhap.Text.ToString());
h.Update();
MessageBox.Show("Sửa Thành Công !", "Thông Báo");
}
private void butThoat_Click(object sender, EventArgs e)
{
Close();
}
}
public partial class PhieuXuat : Form
{
public PhieuXuat()
{
InitializeComponent();
}
private void gridHoaDonXuat_Click(object sender, EventArgs e)
{
string SoHD = gridHoaDonXuat.CurrentRow.Cells["SoHD"].Value.ToString ();
TblHoaDonXuat h = TblHoaDonXuat.LayttHoaDonXuat(SoHD);
txtSoHD.Text = h.SoHD.ToString ();
txtNgayXuat.Text = h.NgayXuat.ToString ();
txtNguoiXuat.Text = h.NguoiXuat.ToString();
txtDienGiai.Text = h.DienGiai.ToString();
GirdHangXuat .DataSource = TblHangXuat .LaydsHangXuat (string.Format("SoHD = {0}", SoHD));
}
private void PhieuXuat_Load(object sender, EventArgs e)
{
List <TblHoaDonXuat > lst = TblHoaDonXuat .LaydsHoaDonXuat("1=1");
gridHoaDonXuat.DataSource = lst;
}
}
}
Bạn đang đọc truyện trên: Truyen247.Pro