Visual C# 2005-O'Reilly

Một phần của tài liệu Ứng dụng công nghệ thông tin trong quản lý hợp tác xã (Trang 61 - 72)

Website: http://www.docs.vn Email : lienhe@docs.vn Tel : 0918.775.368 Mô hình 3 tầng trong chương trình quản lý thu chi của hợp tác xã là:

 Tầng truy xuất cơ sở dữ liệu bao gồm các đối tượng truy xuất đến dữ liệu của các bảng.Ở đây chúng ta tổng hợp các câu lệnh truy xuất đến 1 bảng trong cơ sở dữ liệu thanh 1 đối tượng đúng như bản chất của ngôn ngữ C# là ngôn ngữ lập trình hướng đối tượng

Việc truy xuất cơ sở dũ liệu ở đây là các thao tác insert ,update,delete,select các bản ghi trong table

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using Lopdoituong; namespace dataaccess {

publicclassdahokhau

{

publicSqlConnection conn;

public dahokhau(SqlConnection con) {

this.conn = con; }

publicvoid them(hokhau hk) {

SqlCommand comm = newSqlCommand("themhokhau", conn); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.AddWithValue("@diachi", hk.diachi);

comm.Parameters.AddWithValue("@idchinhsach",Convert.ToInt32(hk.idchinhsach ));

comm.Parameters.AddWithValue("@ldcongich", hk.ldcongich); comm.Parameters.AddWithValue("@lddotuoi", hk.lddotuoi); comm.Parameters.AddWithValue("@mota", hk.mota);

comm.Parameters.AddWithValue("@ngaysinh", hk.ngaysinh); comm.Parameters.AddWithValue("@sochungminhthu", hk.sochungminhthu);

comm.Parameters.AddWithValue("@sokhau", hk.sokhau); comm.Parameters.AddWithValue("@tenchuho", hk.tenchuho); comm.Parameters.AddWithValue("@anh", hk.anh);

comm.ExecuteNonQuery(); }

publicvoid sua(hokhau hk) {

SqlCommand comm = newSqlCommand("suahokhau", conn); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.AddWithValue("@diachi", hk.diachi);

comm.Parameters.AddWithValue("@idchinhsach", hk.idchinhsach); comm.Parameters.AddWithValue("@idhokhau", hk.idhokhau); comm.Parameters.AddWithValue("@ldcongich", hk.ldcongich); comm.Parameters.AddWithValue("@lddotuoi", hk.lddotuoi); comm.Parameters.AddWithValue("@mota", hk.mota);

comm.Parameters.AddWithValue("@ngaysinh", hk.ngaysinh); comm.Parameters.AddWithValue("@sochungminhthu", hk.sochungminhthu);

comm.Parameters.AddWithValue("@sokhau", hk.sokhau); comm.Parameters.AddWithValue("@tenchuho", hk.tenchuho); comm.Parameters.AddWithValue("@anh", hk.anh);

comm.ExecuteNonQuery(); }

publicvoid xoa(hokhau hk) {

SqlCommand comm = newSqlCommand("xoahokhau", conn); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.AddWithValue("@idhokhau", hk.idhokhau); comm.ExecuteNonQuery();

}

publicDataTable selectall() {

SqlCommand comm = newSqlCommand("selectallhokhau", conn); comm.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = newSqlDataAdapter(); da.SelectCommand = comm;

DataSet ds = newDataSet(); da.Fill(ds, "a");

DataTable dt = ds.Tables["a"]; return dt;

}

publicDataTable selectid(string idhokhau) {

SqlCommand comm = newSqlCommand("selectidhokhau", conn); comm.CommandType = CommandType.StoredProcedure;

Website: http://www.docs.vn Email : lienhe@docs.vn Tel : 0918.775.368

SqlDataAdapter da = newSqlDataAdapter(); da.SelectCommand = comm;

DataSet ds = newDataSet(); da.Fill(ds, "a");

DataTable dt = ds.Tables["a"]; return dt;

} } }

 Tầng tính toán: tính toán,tổng hợp dữ liệu mà tầng dữ liệu đưa lên. Như tính tổng tiền thu chi…

using System; using System.Collections.Generic; using System.Text; using dataaccess; using Lopdoituong; using System.Data; using System.Data.SqlClient; namespace logic {

publicclassblhokhau

{

publicSqlConnection conn;

public blhokhau(SqlConnection con) {

this.conn = con; }

publicvoid them(hokhau hk) {

dahokhau dahk = newdahokhau(conn); dahk.them(hk);

}

publicvoid sua(hokhau hk) {

dahokhau dahk = newdahokhau(conn); dahk.sua(hk);

}

publicvoid xoa(hokhau hk) {

dahk.xoa(hk); }

publicDataTable selectall() {

dahokhau dahk = newdahokhau(conn); DataTable dt = dahk.selectall();

return dt; }

} }

 Tầng giao diện: chứa giao diện của chương trình.Đó là các Form chức năng của chương trình.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; using Lopdoituong; using dataaccess; using logic; namespace hoptacxa {

publicpartialclassfrmhokhau : Form

{

publicSqlConnection conn; publicint f;

publichokhau hk;

public frmhokhau(SqlConnection con) {

this.conn = con;

InitializeComponent(); }

Website: http://www.docs.vn Email : lienhe@docs.vn Tel : 0918.775.368

privatevoid btxoa_Click(object sender, EventArgs e) {

if (txtidhokhau.Text != "") {

thietlapdoituong();

if (MessageBox.Show("Bạn có muốn xóa dữ liệu này không", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.No) return;

blhokhau blhk = newblhokhau(conn); blhk.xoa(hk);

} else

{

MessageBox.Show("Bạn hãy chọn bản ghi cần xóa"); }

frmhokhau_Load(sender, e); }

privatevoid frmhokhau_Load(object sender, EventArgs e) { combo(); khoatext(true); annut(true); hienthi(); dgv.Enabled = true; }

publicvoid annut(bool tg) { btnhap.Enabled = tg; btsua.Enabled = tg; btxoa.Enabled = tg; btluu.Enabled = !tg; bthuy.Enabled = !tg; }

publicvoid khoatext(bool tg) { txtdiachi.Enabled = !tg; txtidhokhau.Enabled = !tg; txtldci.Enabled = !tg; txtlddt.Enabled = !tg; txtmota.Enabled = !tg; txtsocmt.Enabled = !tg; txtsokhau.Enabled = !tg; txttenchuho.Enabled=!tg;

cbochinhsach.Enabled = !tg; dtpngaysinh.Enabled = !tg; btanh.Enabled = !tg;

}

publicvoid hienthi() {

blhokhau blhk = newblhokhau(conn); DataTable dt = blhk.selectall();

int i = 0;

dgv.Rows.Clear();

foreach (DataRow dr in dt.Rows) { dgv.Rows.Add(); dgv.Rows[i].Cells[0].Value = dr[0].ToString().Trim(); dgv.Rows[i].Cells[1].Value = dr[1].ToString().Trim(); dgv.Rows[i].Cells[2].Value = dr[2].ToString().Trim(); dgv.Rows[i].Cells[3].Value = dr[3].ToString().Trim(); dgv.Rows[i].Cells[4].Value = dr[4].ToString().Trim(); dgv.Rows[i].Cells[5].Value = dr[5].ToString().Trim(); dgv.Rows[i].Cells[6].Value = dr[6].ToString().Trim(); dgv.Rows[i].Cells[7].Value = dr[7].ToString().Trim(); dgv.Rows[i].Cells[8].Value = dr[8].ToString().Trim(); dgv.Rows[i].Cells[9].Value = dr[9].ToString().Trim(); dgv.Rows[i].Cells[10].Value = dr[10].ToString().Trim(); i = i + 1; } }

publicvoid combo() {

blchinhsach blcs = newblchinhsach(conn); DataTable dt = blcs.selectall();

cbochinhsach.DataSource = dt;

cbochinhsach.DisplayMember = "tenchinhsach"; cbochinhsach.ValueMember = "idchinhsach"; }

publicvoid thietlapdoituong() { hk = newhokhau(); hk.diachi = txtdiachi.Text; hk.idchinhsach = cbochinhsach.SelectedValue.ToString().Trim(); hk.idhokhau = txtidhokhau.Text; hk.ldcongich = txtldci.Text;

Website: http://www.docs.vn Email : lienhe@docs.vn Tel : 0918.775.368 hk.mota = txtmota.Text; hk.ngaysinh = dtpngaysinh.Value.ToShortDateString().ToString().Trim(); hk.sochungminhthu = txtsocmt.Text; hk.sokhau = txtsokhau.Text; hk.tenchuho = txttenchuho.Text; hk.anh = opfd.FileName; }

privatevoid button1_Click(object sender, EventArgs e) { if (cbochinhsach.SelectedIndex != -1) { textBox8.Text = cbochinhsach.SelectedValue.ToString().Trim(); } }

privatevoid btnhap_Click(object sender, EventArgs e) { f = 1; khoatext(false); dgv.Enabled = false; annut(false); }

privatevoid btsua_Click(object sender, EventArgs e) { if (txtidhokhau.Text != "") { f = 2; khoatext(false); annut(false); } else {

MessageBox.Show("Bạn hãy chọn bản ghi cần sửa"); }

}

privatevoid btluu_Click(object sender, EventArgs e) {

thietlapdoituong();

blhokhau blhk = newblhokhau(conn); if (f == 1)

blhk.them(hk); } if (f == 2) { blhk.sua(hk); } frmhokhau_Load(sender, e); }

privatevoid bthuy_Click(object sender, EventArgs e) {

frmhokhau_Load(sender, e); }

privatevoid dataGridView1_CellClick(object sender,

DataGridViewCellEventArgs e) { try { txtidhokhau.Text = dgv.Rows[e.RowIndex].Cells[0].Value.ToString().Trim(); txttenchuho.Text = dgv.Rows[e.RowIndex].Cells[1].Value.ToString().Trim(); dtpngaysinh.Text = dgv.Rows[e.RowIndex].Cells[2].Value.ToString().Trim(); txtsocmt.Text = dgv.Rows[e.RowIndex].Cells[3].Value.ToString().Trim(); txtsokhau.Text = dgv.Rows[e.RowIndex].Cells[4].Value.ToString().Trim(); txtlddt.Text = dgv.Rows[e.RowIndex].Cells[5].Value.ToString().Trim(); txtldci.Text = dgv.Rows[e.RowIndex].Cells[6].Value.ToString().Trim(); txtdiachi.Text = dgv.Rows[e.RowIndex].Cells[7].Value.ToString().Trim(); cbochinhsach.Text = dgv.Rows[e.RowIndex].Cells[8].Value.ToString().Trim(); txtmota.Text = dgv.Rows[e.RowIndex].Cells[9].Value.ToString().Trim(); if (dgv.Rows[e.RowIndex].Cells[10].Value.ToString().Trim() != null) { pictureBox1.Load("file:///" + dgv.Rows[e.RowIndex].Cells[10].Value.ToString().Trim()); }

Website: http://www.docs.vn Email : lienhe@docs.vn Tel : 0918.775.368

{

pictureBox1.Load("file:///" + Directory.GetCurrentDirectory() +

"\\untitled.bmp"); } }

catch { } }

privatevoid btanh_Click(object sender, EventArgs e) {

opfd.Title = "Ảnh";

opfd.Filter = "*.jpg|*.jpg|*.bmp|*.bmp"; if (opfd.ShowDialog() == DialogResult.OK) {

pictureBox1.Load("file:///" + opfd.FileName); }

else

{

pictureBox1.Load("file:///" + Directory.GetCurrentDirectory() +

"\\untitled.bmp"); }

}

privatevoid txtsokhau_KeyPress(object sender, KeyPressEventArgs e) {

if (((e.KeyChar > (char)47) && (e.KeyChar < (char)58) || (e.KeyChar == (char)46)) || (e.KeyChar < (char)32) || (e.KeyChar == 43))

{

e.Handled = false; }

else

{

MessageBox.Show("Không được nhập ký tự này!", "Chỉ được nhập số",

MessageBoxButtons.OK, MessageBoxIcon.Warning); e.Handled = true;

} }

privatevoid txtlddt_KeyPress(object sender, KeyPressEventArgs e) {

if (((e.KeyChar > (char)47) && (e.KeyChar < (char)58) || (e.KeyChar == (char)46)) || (e.KeyChar < (char)32) || (e.KeyChar == 43))

{

e.Handled = false; }

else

{

MessageBox.Show("Không được nhập ký tự này!", "Chỉ được nhập số",

MessageBoxButtons.OK, MessageBoxIcon.Warning); e.Handled = true;

} }

privatevoid txtldci_KeyPress(object sender, KeyPressEventArgs e) {

if (((e.KeyChar > (char)47) && (e.KeyChar < (char)58) || (e.KeyChar == (char)46)) || (e.KeyChar < (char)32) || (e.KeyChar == 43))

{

e.Handled = false; }

else

{

MessageBox.Show("Không được nhập ký tự này!", "Chỉ được nhập số",

MessageBoxButtons.OK, MessageBoxIcon.Warning); e.Handled = true;

} } } }

 Tầng các đối tượng mà ta thao tác đến

using System;

using System.Collections.Generic;

using System.Text;

namespace Lopdoituong {

publicclasshokhau

{

publicstring idhokhau ; publicstring tenchuho ; publicstring ngaysinh ;

publicstring sochungminhthu ; publicstring sokhau ;

Website: http://www.docs.vn Email : lienhe@docs.vn Tel : 0918.775.368

publicstring ldcongich ; publicstring diachi ; publicstring idchinhsach ; publicstring mota ;

publicstring anh; }

Một phần của tài liệu Ứng dụng công nghệ thông tin trong quản lý hợp tác xã (Trang 61 - 72)