|
|
|
c# 2008 ช่วยอธิบายโค้ดแต่ละบรรทัดให้หน่อยนะคับ รบกวนด้วยคับ |
|
|
|
|
|
|
|
Code (C#)
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.OleDb;
namespace musical
{
public partial class brand : Form
{
OleDbConnection Conn; //connet กับ database
OleDbCommand com; //ประมวลผลคำสั่ง sql
StringBuilder sb;
OleDbTransaction tr; //การเปลี่ยนแปลงข้อมูล
OleDbDataReader dr; //เก็บผลการคิวรี่ข้อมูลจาก command
public static string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\dbmusical.accdb";
public int CountRecord;
public string TempID;
public string TrimID;
public int TempNum;
AutoCompleteStringCollection acBrandID = new AutoCompleteStringCollection();
AutoCompleteStringCollection acBrandName = new AutoCompleteStringCollection();
public static string brandd, branddd;
public brand()
{
InitializeComponent();
}
public void Connect()
{
try
{
Conn = new OleDbConnection();
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void SetAutoCompleteMode()
{
txtBrandID.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtBrandID.AutoCompleteCustomSource = acBrandID;
txtBrandID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtBrandName.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtBrandName.AutoCompleteCustomSource = acBrandName;
txtBrandName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
public void ShowDatabrand()
{
Connect();
string brandSQL = "SELECT * FROM brand";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = brandSQL;
com.Connection = Conn;
dr = com.ExecuteReader();
if (dr.HasRows)
{
DataTable dt = new DataTable();
dt.Load(dr);
//โหลด ฟิว ชื่อรหัสยี่ห้อและชื่อยี่ห้อ มาไว้ที่ text
foreach (DataRow drw in dt.Rows)
{
acBrandID.Add(drw["brandID"].ToString());
}
foreach (DataRow drw in dt.Rows)
{
acBrandName.Add(drw["brandName"].ToString());
}
}
else
{
MessageBox.Show("ไม่พบข้อมูล");
return;
}
dr.Close();
this.Cursor = Cursors.Default;
SetAutoCompleteMode();
}
public void ShowCus()
{
string brandSQL = "SELECT * FROM brand WHERE " + brandd + "= @brandd";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = brandSQL;
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@brandd", OleDbType.VarChar).Value = branddd.ToString();
dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
txtBrandID.Text = dr["brandID"].ToString();
txtBrandName.Text = dr["brandName"].ToString();
ShowDatabrand();
}
else
{
MessageBox.Show("ไม่พบข้อมูล");
ClearData();
return;
}
dr.Close();
}
private void showdata()
{
// ฟังก์ชัน แสดงข้อมูล
Connect();
string brandSQL;
brandSQL = "Select * from brand";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = brandSQL;
com.Connection = Conn;
dr = com.ExecuteReader();
if (dr.HasRows)
{
DataTable dt = new DataTable();
dt.Load(dr);
dgvbrand.DataSource = dt;
}
else
{
MessageBox.Show("ไม่พบข้อมูล");
return;
}
dr.Close();
// ----กำหนดหัวกับขนาดของ datagridview--------------
dgvbrand.Columns[0].HeaderText = "รหัสยี่ห้อสินค้า";
dgvbrand.Columns[1].HeaderText = "ชื่อยี่ห้อสินค้า";
dgvbrand.Columns[0].Width = 100;
dgvbrand.Columns[1].Width = 200;
}
private void ClearData()
{
BtnSearch.Enabled = true; //ค้นหา
BtnSearchb.Enabled = true;
BtnSearchIDb.Enabled = true;
BtnAdd.Enabled = true; //เพิ่ม
BtnDelete.Enabled = true; //ลบ
BtnUpdate.Enabled = true; //แก้ไข
BtnSave.Enabled = false; //บันทึก
BtnCancle.Enabled = false; //ยกเลิก
txtBrandID.Clear();
txtBrandName.Clear();
txtSearch.Clear();
}
public void CreateID()//ฟังก์ชั่นออกรหัสยี่ห้อสินค้า
{
Connect();
string brandSQL = "SELECT * FROM brand ORDER BY BrandID";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = brandSQL;
com.Connection = Conn;
dr = com.ExecuteReader();
if (dr.HasRows)
{
DataTable dt = new DataTable();
dt.Load(dr);
CountRecord = dt.Rows.Count; //นับจำนวนเรคคอร์ด
TempID = dt.Rows[CountRecord - 1]["BrandID"].ToString();
TrimID = TempID.Substring(1);
TempNum = Convert.ToInt32(TrimID);
}
dr.Close();
TempNum = Convert.ToInt32(TempNum) + 1;
if (TempNum <= 9)
txtBrandID.Text = "b00" + TempNum.ToString();
else if (TempNum <= 99)
txtBrandID.Text = "b0" + TempNum.ToString();
else if (TempNum <= 999)
txtBrandID.Text = "b" + TempNum.ToString();
}
private void ClearText()
{
txtBrandID.Clear();
txtBrandName.Clear();
}
public bool check = true;
private void CheckText()//ตัวแปรเช็ค Text
{
check = true;
if (txtBrandName.Text == "")
{
MessageBox.Show("คุณยังไม่ได้กรอกชื่อยี่ห้อสินค้า");
txtBrandName.Focus();
check = false;
return;
}
}
private void txtSearch_KeyDown(object sender, KeyEventArgs e)
{
}
private void txtbrandName_TextChanged(object sender, EventArgs e)
{
}
//datagrid
private void dgvbrand_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
txtBrandID.ReadOnly = true;
if (e.RowIndex == -1)
{
return;
}
txtBrandID.Text = dgvbrand.Rows[e.RowIndex].Cells["BrandID"].Value.ToString();
txtBrandName.Text = dgvbrand.Rows[e.RowIndex].Cells["BrandName"].Value.ToString();
txtBrandName.Focus();
txtBrandName.SelectAll();
}
private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
{
txtBrandID.Text = listView1.FocusedItem.SubItems[1].Text;
txtBrandName.Text = listView1.FocusedItem.SubItems[2].Text;
}
//คลิกปุ่มเพิ่ม
private void BtnAdd_Click(object sender, EventArgs e)
{
ClearData();
CreateID();
BtnSearch.Enabled = false;
BtnSearchb.Enabled = false;
BtnSearchIDb.Enabled = false;
BtnAdd.Enabled = false;
BtnDelete.Enabled = false;
BtnUpdate.Enabled = false;
BtnSave.Enabled = true;
BtnCancle.Enabled = true;
txtBrandID.ReadOnly = false;
txtBrandName.Focus();
}
//ลบ
private void BtnDelete_Click(object sender, EventArgs e)
{
txtBrandID.ReadOnly = false;
if (txtBrandID.Text == "")
{
MessageBox.Show("คุณยังไม่ได้เลือกรายการที่ต้องการลบ");
txtBrandID.Focus();
return;
}
Connect();
string brandSQL = "SELECT * FROM product WHERE BrandID=@BrandID";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = brandSQL;
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@BrandID", OleDbType.VarChar).Value = txtBrandID.Text;
dr = com.ExecuteReader();
if (dr.HasRows)
{
MessageBox.Show("ไม่สามารถลบข้อมูลนี้ได้", "รายงานการลบ");
ClearData();
dr.Close();
}
else
{
tr = Conn.BeginTransaction();
sb = new StringBuilder();
sb.Append("DELETE FROM brand WHERE BrandID=@BrandID");
string sqlDel = sb.ToString();
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = sqlDel;
com.Connection = Conn;
com.Transaction = tr;
com.Parameters.Clear();
com.Parameters.Add("@BrandID", OleDbType.VarChar).Value = txtBrandID.Text;
int result = com.ExecuteNonQuery();
if (result == 0)
{
tr.Rollback();
MessageBox.Show("ไม่พบรายการที่ต้องการลบ", "รายงานการลบ");
txtBrandID.Focus();
return;
}
else
{
tr.Commit();
MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว", "รายงานการลบ");
}
txtBrandID.ReadOnly = false;
showdata();
ClearData();
}
}
//แก้ไข
private void BtnUpdate_Click(object sender, EventArgs e)
{
txtBrandID.ReadOnly = false;
CheckText();
if (check != false)
{
tr = Conn.BeginTransaction();
try
{
com = new OleDbCommand();
sb = new StringBuilder();
sb.Append("UPDATE brand SET BrandID=@BrandID,BrandName=@BrandName WHERE BrandID=@BrandID");
string sqladd = "";
sqladd = sb.ToString();
com.CommandType = CommandType.Text;
com.CommandText = sqladd;
com.Connection = Conn;
com.Transaction = tr;
com.Parameters.Clear();
com.Parameters.Add("@BrandID", OleDbType.VarChar).Value = txtBrandID.Text;
com.Parameters.Add("@BrandName", OleDbType.VarChar).Value = txtBrandName.Text;
com.ExecuteNonQuery();
int result = com.ExecuteNonQuery();
if (result == 0)
{
tr.Rollback();
MessageBox.Show("รหัสที่คุณกรอกมาไม่ถูกต้อง");
txtBrandID.Focus();
return;
}
else
{
if (MessageBox.Show("คุณต้องการแก้ไขข้อมูลใช่หรือไม่", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
tr.Rollback();
return;
}
tr.Commit();
MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว");
}
}
catch (Exception ex)
{
tr.Rollback();
MessageBox.Show("รายงานผิดพลาด" + ex.Message, "ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
showdata();
ClearData();
}
}
//บันทึก
private void BtnSave_Click_1(object sender, EventArgs e)
{
txtBrandID.ReadOnly = false;
txtBrandID.ReadOnly = false;
CheckText();
if (check != false)
{
string brandSQL = "SELECT * FROM brand WHERE BrandID = @BrandID";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = brandSQL;
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@BrandID", OleDbType.VarChar).Value = txtBrandID.Text;
dr = com.ExecuteReader();
if (dr.HasRows)
{
MessageBox.Show("รหัสนี้มีอยู่แล้ว");
txtBrandID.Focus();
return;
}
dr.Close();
brandSQL = "SELECT * FROM brand WHERE BrandName = @BrandName";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = brandSQL;
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@BrandName", OleDbType.VarChar).Value = txtBrandName.Text;
dr = com.ExecuteReader();
if (dr.HasRows)
{
MessageBox.Show("ชื่อประเภทสินค้านี้มีแล้ว");
txtBrandName.Focus();
return;
}
dr.Close();
tr = Conn.BeginTransaction();
try
{
com = new OleDbCommand();
sb = new StringBuilder();
sb.Append("INSERT INTO brand(BrandID,BrandName) VALUES (@BrandID,@BrandName)");
string sqladd = "";
sqladd = sb.ToString();
com.CommandType = CommandType.Text;
com.CommandText = sqladd;
com.Connection = Conn;
com.Transaction = tr;
com.Parameters.Clear();
com.Parameters.Add("@BrandID", OleDbType.VarChar).Value = txtBrandID.Text;
com.Parameters.Add("@BrandName", OleDbType.VarChar).Value = txtBrandName.Text;
com.ExecuteNonQuery();
tr.Commit();
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "รายงานการบันทึก", MessageBoxButtons.OK);
}
catch (Exception ex)
{
tr.Rollback();
MessageBox.Show("รายงานการผิดพลาด" + ex.Message, "ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
showdata();
ClearData();
}
}
//ยกเลิก
private void BtnCancle_Click(object sender, EventArgs e)
{
txtBrandID.ReadOnly = false;
listView1.Items.Clear();
ClearData();
ClearText();
}
private void brand_Load_1(object sender, EventArgs e)
{
Connect();
showdata();
ClearData();
ShowDatabrand();
listView1.Clear();
listView1.Columns.Add("ลำดับ");
listView1.Columns.Add("รหัสยี่ห้อสินค้า", 100, HorizontalAlignment.Center);
listView1.Columns.Add("ชื่อยี่ห้อสินค้า", 90, HorizontalAlignment.Left);
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
}
//ค้นหา
private void BtnSearch_Click(object sender, EventArgs e)
{
txtBrandID.ReadOnly = true;
BtnAdd.Enabled = false;
BtnDelete.Enabled = true;
BtnUpdate.Enabled = true;
BtnSave.Enabled = false;
BtnCancle.Enabled = true;
if (txtSearch.Text == "")
{
MessageBox.Show("กรุณากรอกข้อมูลที่ต้องการค้นหา");
txtSearch.Focus();
showdata();
}
if (CmbSearch.Text == "รหัสยี่ห้อสินค้า")
{
Connect();
string brandSQL = "SELECT * FROM brand WHERE BrandID=@BrandID";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = brandSQL;
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@BrandID", OleDbType.VarChar).Value = txtSearch.Text;
dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
txtBrandID.Text = dr["BrandID"].ToString();
txtBrandName.Text = dr["BrandName"].ToString();
}
else
{
MessageBox.Show("ไม่พบข้อมูลที่ต้องการค้นหา");
showdata();
return;
}
dr.Close();
}
if (CmbSearch.Text == "ชื่อยี่ห้อสินค้า")
{
Connect();
string brandSQL = "SELECT * FROM brand WHERE BrandName=@BrandName";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = brandSQL;
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@BrandName", OleDbType.VarChar).Value = txtSearch.Text;
dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
txtBrandID.Text = dr["BrandID"].ToString();
txtBrandName.Text = dr["BrandName"].ToString();
}
else
{
MessageBox.Show("ไม่พบข้อมูลที่ต้องการค้นหา");
showdata();
return;
}
dr.Close();
}
}
private void BtnSearchIDb_Click(object sender, EventArgs e)
{
txtBrandID.ReadOnly = false;
listView1.Visible = true;
if (txtBrandID.Text == "")
{
MessageBox.Show("กรุณากรอกข้อมูลที่ต้องการค้นหา");
txtBrandID.Focus();
showdata();
return;
}
string BrandID, BrandName;
string customer1SQL = "SELECT * FROM brand WHERE BrandID= @BrandID";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = customer1SQL;
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@BrandID", OleDbType.VarChar).Value = txtBrandID.Text;
dr = com.ExecuteReader();
if (dr.HasRows)
{
DataTable dt = new DataTable();
dt.Load(dr);
int CountID = dt.Rows.Count;
int Num = 1;
for (int i = 0; i <= CountID - 1; i++)
{
BrandID = dt.Rows[i]["BrandID"].ToString();
BrandName = dt.Rows[i]["BrandName"].ToString();
ListViewItem lvi;
string[] anyData;
anyData = new string[] { Num.ToString(), BrandID, BrandName };
lvi = new ListViewItem(anyData);
listView1.Items.Add(lvi);
Num++;
}
}
else
{
MessageBox.Show("ไม่พบข้อมูลที่ต้องการค้นหา");
txtBrandID.Focus();
return;
}
dr.Close();
}
private void BtnSearchb_Click(object sender, EventArgs e)
{
txtBrandID.ReadOnly = false;
listView1.Visible = true;
if (txtBrandName.Text == "")
{
MessageBox.Show("กรุณากรอกข้อมูลที่ต้องการค้นหา");
txtBrandName.Focus();
showdata();
return;
}
string BrandID, BrandName;
string customer1SQL = "SELECT * FROM brand WHERE BrandName= @BrandName";
com = new OleDbCommand();
com.CommandType = CommandType.Text;
com.CommandText = customer1SQL;
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@BrandName", OleDbType.VarChar).Value = txtBrandName.Text;
dr = com.ExecuteReader();
if (dr.HasRows)
{
DataTable dt = new DataTable();
dt.Load(dr);
int CountID = dt.Rows.Count;
int Num = 1;
for (int i = 0; i <= CountID - 1; i++)
{
BrandID = dt.Rows[i]["BrandID"].ToString();
BrandName = dt.Rows[i]["BrandName"].ToString();
ListViewItem lvi;
string[] anyData;
anyData = new string[] { Num.ToString(), BrandID, BrandName };
lvi = new ListViewItem(anyData);
listView1.Items.Add(lvi);
Num++;
}
}
else
{
MessageBox.Show("ไม่พบข้อมูลที่ต้องการค้นหา");
txtBrandName.Focus();
return;
}
dr.Close();
}
private void CmbSearch_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
type page2 = new type();
page2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
supplier sup = new supplier();
sup.Show();
}
private void button3_Click(object sender, EventArgs e)
{
customer cus = new customer();
cus.Show();
}
}
}
หาคนใจดี ช่วยอธิบายแต่ละบรรทัดให้หน่อยได้ไหมคับ ไม่เข้าใจเลย จะต้องเอาไปสอบด้วย รบกวนหน่อยนะคับ
Tag : .NET, Ms Access, C#, VS 2008 (.NET 3.x)
|
|
|
|
|
|
Date :
2011-09-04 01:35:54 |
By :
MakuraZa |
View :
1387 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
669 กว่าบรรทัดน่ะครับ
|
|
|
|
|
Date :
2011-09-04 08:13:20 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ยาวขนาดนี้ใครจะไปอธิบายไหว
|
|
|
|
|
Date :
2011-09-04 10:36:30 |
By :
Marcuz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|