MÃ VIẾT PHẦN MẾM QUẢN LÝ HỘ KHẦU BẰNGC# 1.. mã phần nhập dữ liệu using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb;
Trang 1MÃ VIẾT PHẦN MẾM QUẢN LÝ HỘ KHẦU BẰNG
C#
1 mã phần nhập dữ liệu
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Media;
using System.Windows.Forms;
Trang 2namespace vidu5
{
public partial class nhaplieu : Form
{
public OleDbConnection mycon;
public OleDbDataAdapter myda;
public DataSet myds;
public DataTable mydt;
DataTable dtt;
DataView dw;
OleDbDataAdapter da;
Data dt = new Data();
int t, u;
public nhaplieu()
{
InitializeComponent();
}
private void nhaplieu_Load(object sender, EventArgs e)
{
dt.ketnoi();
hien();
Demdong();
hiennhap(false);
hienbutton(true);
this.CenterToScreen();// canh giữa màn hình
tim();
}
//code không cho form di
chuyen=============================================
===============================
Trang 3protected override void WndProc(ref Message m)
{
const int WM_NCLBUTTONDOWN = 161;
const int WM_SYSCOMMAND = 274;
const int HTCAPTION = 2;
const int SC_MOVE = 61456;
if ((m.Msg == WM_SYSCOMMAND) &&
(m.WParam.ToInt32() == SC_MOVE))
{
return;
}
if ((m.Msg == WM_NCLBUTTONDOWN) &&
(m.WParam.ToInt32() == HTCAPTION))
{
return;
}
base.WndProc(ref m);
}
//====================================
private void Demdong()
{
//NếMu tôn tại dữ liệu trong
datagridview (trừ dòng trôMng ra nhe)
if (dtgcutru.RowCount > 0)
{
int iCount = -1;
for (int i = 0; i <=
dtgcutru.RowCount - 1; i++)
iCount++;
this.LB_TONG.Text =
iCount.ToString();
int iDem = 0;
for (int i = 0; i <=
dtgcutru.RowCount - 1; i++)
iDem++;
this.txtstt.Text = iDem.ToString();
Trang 4}
else
{ MessageBox.Show("Không tìm được tến trong danh sách"); }
}
//================================================ private void lammoi()
{
txtstt.Clear();
txtsonha.Clear();
txthoten.Clear();
txtqhch.Clear();
txtnoisinh.Clear();
txtdantoc.Clear();
txttongiao.Clear();
txtnghenghiep.Clear();
txtcmnd.Clear();
}
//==================================
private void hiennhap(bool edit)
{
txtstt.Enabled = edit;
}
//=================================
private void hienbutton(bool edit)
{
btnthem.Visible = edit;
btnsua.Visible = edit;
btnxoa.Visible = edit;
btnluu.Visible = !edit;
btnhuy.Visible = !edit;
}
//==============================
private void hien()
Trang 5{
dt.datagrid("SELECT * FROM tblcutru"); this.dtgcutru.DataSource = dt.mydt;
this.dtgcutru.AutoGenerateColumns = false;
}
//===============================
private void btnthem_Click_1(object sender, EventArgs e)
{
lammoi();
hiennhap(true);
hienbutton(false);
txtsonha.Focus();
Demdong();
t = 0;
}
private void btnluu_Click_1(object sender, EventArgs e)
{
if (txtstt.TextLength == 0 ||
txtsonha.TextLength == 0 || txthoten.TextLength ==
0 || txtqhch.TextLength == 0 ||
txtnoisinh.TextLength == 0 || txtdantoc.TextLength
== 0 || txttongiao.TextLength == 0 ||
txtnghenghiep.TextLength == 0)
{
MessageBox.Show("THÔNG TIN KHÔNG ĐƯỢC BOd TRÔMNG");
}
Trang 6
else
{
try
{
if (t == 0)
{
dt.command("INSERT INTO tblcutru(stt,sonha,ap,hoten,qhch,namsinh,gioitinh,n oisinh,dantoc,tongiao,nghenghiep,cmnd)" +
"VALUES('" + txtstt.Text +
"','" + txtsonha.Text + "','" + cbbap.Text + "','"
+ txthoten.Text + "','" + txtqhch.Text + "','" + dtnamsinh.Value.ToShortDateString() + "', '" +
cbbgioitinh.Text + "','" + txtnoisinh.Text + "','"
+ txtdantoc.Text + "','" + txttongiao.Text + "','"
+ txtnghenghiep.Text + "','" + txtcmnd.Text +
"')");
dt.xoamyds();
hien();
Demdong();
hienbutton(true);
hiennhap(false);
}
else if (t == 1)
{
dt.command("UPDATE tblcutru SET stt='" + txtstt.Text + "',sonha='" +
txtsonha.Text + "',ap='" + cbbap.Text + "',hoten='"
+ txthoten.Text + "',qhch='" + txtqhch.Text +
"',namsinh='" + dtnamsinh.Value.ToShortDateString() + "',gioitinh='" + cbbgioitinh.Text + "',
noisinh='" + txtnoisinh.Text + "', dantoc='" +
txtdantoc.Text + "', tongiao='" + txttongiao.Text +
"', nghenghiep='" + txtnghenghiep.Text + "',
Trang 7cmnd='" + txtcmnd.Text + "' WHERE stt='" +
txtstt.Text + "' ");
dt.xoamyds();
hien();
hienbutton(true);
hiennhap(false);
t = -1;
}
}
catch
{
MessageBox.Show("SôM thứ tự " + txtstt.Text + " đã tôn tại, nhập lại sôM khác");
}
}
}
private void btnxoa_Click(object sender, EventArgs e)
{
if (u != 1)
MessageBox.Show("Chưa chọn dữ
liệu!", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
else if (MessageBox.Show("Bạn có muôMn xóa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
dt.command("DELETE FROM tblcutru WHERE stt='" + txtstt.Text + "' ");
dt.xoamyds();
hien();
Trang 8Demdong();
lammoi();
}
}
private void btnsua_Click_1(object sender, EventArgs e)
{
hienbutton(false);
hiennhap(true);
txtstt.Enabled = false;
t = 1;
}
private void btnhuy_Click_1(object sender, EventArgs e)
{
lammoi();
hiennhap(false);
hienbutton(true);
}
private void btnthoat_Click(object sender, EventArgs e)
{
this.Close();
Form1 nhaplieu = new Form1();
nhaplieu.Show();
}
// vô hiệu hóa nút
X==========================
private const int CP_NOCLOSE_BUTTON =
0x200;
protected override CreateParams
CreateParams
{
get
{
Trang 9CreateParams myCp =
base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp;
}
}
private void
dtgcutru_CellContentClick_1(object sender,
DataGridViewCellEventArgs e)
{
try
{
if (t == 0 || t == 1)
u = 1;
int row = e.RowIndex;
txtstt.Text = dt.mydt.Rows[row] ["stt"].ToString();
txtsonha.Text = dt.mydt.Rows[row] ["sonha"].ToString();
cbbap.Text = dt.mydt.Rows[row]
["ap"].ToString();
txthoten.Text = dt.mydt.Rows[row] ["hoten"].ToString();
txtqhch.Text = dt.mydt.Rows[row] ["qhch"].ToString();
dtnamsinh.Text = dt.mydt.Rows[row] ["namsinh"].ToString();
cbbgioitinh.Text =
dt.mydt.Rows[row]["gioitinh"].ToString();
txtnoisinh.Text = dt.mydt.Rows[row] ["noisinh"].ToString();
txtdantoc.Text = dt.mydt.Rows[row] ["dantoc"].ToString();
txttongiao.Text = dt.mydt.Rows[row] ["tongiao"].ToString();
txtnghenghiep.Text =
dt.mydt.Rows[row]["nghenghiep"].ToString();
Trang 10txtcmnd.Text = dt.mydt.Rows[row] ["cmnd"].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//=================================================
============================
#region Enter xuôMng dòng
private void txtsonha_KeyPress_1(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
cbbap.Focus();
}
}
private void cbbap_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txthoten.Focus();
}
}
private void txthoten_KeyPress(object
sender, KeyPressEventArgs e)
{
Trang 11e.KeyChar =
char.Parse(e.KeyChar.ToString().ToUpper());
if (e.KeyChar == 13)
{
txtqhch.Focus();
}
}
private void txtqhch_KeyPress(object
sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
dtnamsinh.Focus();
}
}
private void cbbgioitinh_KeyPress_1(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtnoisinh.Focus();
}
}
private void txtnoisinh_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtdantoc.Focus();
}
}
private void txtdantoc_KeyPress(object sender, KeyPressEventArgs e)
{
Trang 12if (e.KeyChar == 13)
{
txttongiao.Focus();
}
}
private void txttongiao_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtnghenghiep.Focus();
}
}
private void
txtnghenghiep_KeyPress_1(object sender,
KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtcmnd.Focus();
}
}
private void txtcmnd_KeyPress(object
sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
button1.Focus();
}
}
private void btnluu_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
btnluu.Focus();
Trang 13}
}
private void dtnamsinh_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
cbbgioitinh.Focus();
}
}
private void btnthem_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
btnthem.Focus();
}
}
#endregion
//====================================== #region các mennu
private void
lPAToolStripMenuItem_Click_1(object sender, EventArgs e)
{
LPA nhaplieu = new LPA();
nhaplieu.Show();
}
private void
lAAToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Trang 14LAA nhaplieu = new LAA();
nhaplieu.Show();
}
private void
lTToolStripMenuItem_Click_1(object sender, EventArgs e)
{
LT nhaplieu = new LT();
nhaplieu.Show();
}
private void
aPToolStripMenuItem_Click_1(object sender, EventArgs e)
{
AP nhaplieu = new AP();
nhaplieu.Show();
}
private void
tDToolStripMenuItem_Click_1(object sender, EventArgs e)
{
TD nhaplieu = new TD();
nhaplieu.Show();
}
private void
pĐToolStripMenuItem_Click_1(object sender, EventArgs e)
{
PĐ nhaplieu = new PĐ();
nhaplieu.Show();
}
private void
tÌMTHEOHỌTÊNToolStripMenuItem_Click_1(object sender, EventArgs e)
Trang 15{
frmtim nhaplieu = new frmtim();
nhaplieu.Show();
}
private void
tÌMTHEOSÔMNHÀToolStripMenuItem_Click_1(object
sender, EventArgs e)
{
frmtim_sonha nhaplieu = new
frmtim_sonha();
nhaplieu.Show();
}
private void
sAOLƯUDLToolStripMenuItem_Click_1(object sender, EventArgs e)
{
string dbFileName = "cutru.mdb";
string CurrentDatabasePath =
Path.Combine(Environment.CurrentDirectory,
dbFileName);
string backTimeStamp =
Path.GetFileNameWithoutExtension(dbFileName) + "_"
+ DateTime.Now.Year.ToString("yyyy-MM-dd") +
Path.GetExtension(dbFileName);
string destFileName = backTimeStamp + dbFileName;
FolderBrowserDialog fbd = new
FolderBrowserDialog();
if (fbd.ShowDialog() ==
DialogResult.OK)
{
string PathtobackUp =
fbd.SelectedPath.ToString();
destFileName =
Path.Combine(PathtobackUp, destFileName);
File.Copy(CurrentDatabasePath,
destFileName, true);
Trang 16MessageBox.Show("successful Backup!
");
}
}
private void
tÁCGIAdToolStripMenuItem_Click_1(object sender,
EventArgs e)
{
Thongtintacgia nhaplieu = new
Thongtintacgia();
nhaplieu.Show();
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
if (txtsonha.TextLength == 0 ||
txthoten.TextLength == 0)
{
MessageBox.Show("CHƯA CÓ TÊN ĐÊd KIÊdM TRA");
}
else
{
try
{
kiemtratrungdulieu();
}
catch
{}
}
}
public void kiemtratrungdulieu()
{
Trang 17for (int i = 0; i < dtgcutru.Rows.Count
- 1; i++)
{
if (txtsonha.Text ==
dtgcutru.Rows[i].Cells[1].Value.ToString() &
cbbap.Text ==
dtgcutru.Rows[i].Cells[2].Value.ToString() &
txthoten.Text ==
dtgcutru.Rows[i].Cells[3].Value.ToString())
{
MessageBox.Show("HỌ VÀ TÊN: " + txthoten.Text +" ĐÃ CÓ TRONG TÀNG THƯ, ĐÊ NGHỊ XEM LẠI!");
}
}
}
private void
tÌMTHEONĂMSINHToolStripMenuItem_Click(object
sender, EventArgs e)
{
timnamsinh nhaplieu = new timnamsinh(); nhaplieu.Show();
}
private void tim()
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;D ata Source=cutru.mdb");
da = new OleDbDataAdapter("select * from tblcutru", con);
dtt = new DataTable("tblcutru");
da.Fill(dtt);
Trang 18dw = new DataView(dtt);
dtgcutru.DataSource = dw;
string timhoten = txthoten.Text;
dw.RowFilter = "hoten like'" +
txthoten.Text + "%' ";
dtgcutru.DataSource = dw;
}
}
}
2 Phần kết nối C# với access2003
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace vidu5
{
#region "Access"
class Data
{
public OleDbConnection mycon;
public OleDbDataAdapter myda;
public DataSet myds;
public DataTable mydt;
private OleDbCommand mycom;
public void ketnoi()
{
try
{
mycon = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;D ata Source=cutru.mdb");
Trang 19mycon.Open();
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
}
public void ngatketnoi()
{
mycon.Close();
}
public void command(string luusql)
{
mycom = new OleDbCommand(luusql, mycon);
mycom.ExecuteNonQuery();
}
public void xoamyds()
{
myds.Clear();
}
public DataTable taobang(string sql)
{
DataTable mydt = new DataTable();
OleDbDataAdapter myda = new
OleDbDataAdapter();
myda.Fill(mydt);
return (mydt);
}
public void datagrid(string all)
{
myda = new OleDbDataAdapter(all, mycon);
myds = new DataSet();
myda.Fill(myds, "tblcutru");
mydt = myds.Tables["tblcutru"]; }
}