ผมเขียนเพิ่มภาพลงฐานข้อมูล MySQL ได้แต่ ดึงมาแสดงไม่ได้อะคับ ไม่ทราบติดตรงใหนคับ โค๊ด ด้านล่างนะคับ ช่วยดูให้หน่อยนะคับ เอามาทั้งไฟล์เลย
System;
using
System.IO;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
MySql.Data;
using
MySql.Data.MySqlClient;
namespace
{
slaughterhousepublic partial class frmCustomer : Form
{
{
InitializeComponent();
}
public frmCustomer()MySqlConnection MyCon = new MySqlConnection();MySqlDataAdapter Da;DataSet Ds = new DataSet();BindingSource bs = new BindingSource();FileStream FS;byte[] CurrentImage;string ImageFileName = "";bool IsFindImage = false;string CurrentCustomer = "";bool IsFind = false;
#region
{
sqlImage =
sqlImage +=
{
Ds.Tables[
}
Da.SelectCommand.CommandText = sqlImage;
Da.Fill(Ds,
{
IsFindImage =
CurrentImage = (
CustPic.Image =
}
ShowImageprivate void ShowImage()string sqlImage;"SELECT customer_pic FROM tb_customer";" WHERE customer_id='"+CurrentCustomer+"' ";if (IsFindImage == true)"CurrentImage"].Clear();"CurrentImage");if(Ds.Tables["CurrentImage"].Rows.Count !=0)true;byte[])Ds.Tables["CurrentImage"].Rows[0]["customer_pic"];MemoryStream MS = new MemoryStream(CurrentImage, true);Image.FromStream(MS);else
{
IsFindImage =
}
}
false;
#endregion
#region
{
{
CS.Font =
DGAllCustomer.ColumnHeadersDefaultCellStyle = CS;
DGAllCustomer.Columns[0].HeaderText =
DGAllCustomer.Columns[1].HeaderText =
DGAllCustomer.Columns[2].HeaderText =
DGAllCustomer.Columns[3].HeaderText =
DGAllCustomer.Columns[4].HeaderText =
DGAllCustomer.Columns[5].HeaderText =
DGAllCustomer.Columns[6].HeaderText =
DGAllCustomer.Columns[7].HeaderText =
DGAllCustomer.Columns[8].HeaderText =
DGAllCustomer.Columns[0].Width = 80;
DGAllCustomer.Columns[1].Width = 110;
DGAllCustomer.Columns[2].Width = 160;
DGAllCustomer.Columns[3].Width = 170;
DGAllCustomer.Columns[4].Width = 100;
DGAllCustomer.Columns[5].Width = 160;
DGAllCustomer.Columns[6].Width = 100;
DGAllCustomer.Columns[7].Width = 60;
DGAllCustomer.Columns[8].Width = 90;
}
}
ฟังก์ชันฟอร์แม็ตดาต้ากริดprivate void FormatAllCustomer()if (DGAllCustomer.Rows.Count != 0)DataGridViewCellStyle CS = new DataGridViewCellStyle();new Font("Ms Sans Serif", 10, FontStyle.Bold);"รหัสลูกค้า";"คำนำหน้าชื่อ";"ชื่อสกุล";"รหัสประจำตัวประชาชน";"วันที่สมัคร";"การศึกษา";"วันเกิด";"อายุ";"เบอร์โทร";
#endregion
#region
{
DataGridCellColorprivate void DataGridCellColor()//for (int r = 0; r < DGAllCustomer.Rows.Count; r++)
//{
//}
}
#endregion
#region
{
sqlCust =
sqlCust +=
sqlCust +=
sqlCust +=
{
Ds.Tables[
}
Da =
Da.Fill(Ds,
{
IsFind =
DGAllCustomer.ReadOnly =
DGAllCustomer.DataSource = Ds.Tables[
}
ฟังก์แสดงรายการลูกค้าprivate void ShowAllCustomer()string sqlCust;"SELECT customer_code,customer_prefix,customer_name,customer_idcard,";"customer_regis,customer_education,customer_birth,";"customer_age,customer_phone";" FROM tb_customer";if (IsFind == true)"tb_customer"].Clear();new MySqlDataAdapter(sqlCust, MyCon);"tb_customer");if (Ds.Tables["tb_customer"].Rows.Count != 0)true;true;"tb_customer"];else
{
IsFind =
}
}
false;
#endregion
#region
{
txtName.Text =
txtCode.Text =
txtAge.Text =
txtEducation.Text =
txtIDCode.Text =
txtPhone.Text =
cboPrefix.Text =
dtpBirth.Value = ToDay.AddYears(-20);
dtpRegister.Value = ToDay;
txtCode.Focus();
txtCode.SelectAll();
ImageFileName =
CustPic.SizeMode =
CustPic.Image = CustPic.ErrorImage;
}
เคลียร์ข้อมูลบนฟอร์มprivate void ClearAllData()"";"";"";"";"";"";"คำนำหน้า";DateTime ToDay = DateTime.Now;"";PictureBoxSizeMode.StretchImage;
#endregion
#region
{
txtAge.Enabled =
cboPrefix.Enabled =
txtCode.Enabled =
txtEducation.Enabled =
txtIDCode.Enabled =
txtName.Enabled =
txtPhone.Enabled =
dtpBirth.Enabled =
dtpRegister.Enabled =
}
Disible All Controlprivate void DisibleAllControl()false;false;false;false;false;false;false;false;false;
#endregion
#region
{
txtAge.Enabled =
cboPrefix.Enabled =
txtCode.Enabled =
txtEducation.Enabled =
txtIDCode.Enabled =
txtName.Enabled =
txtPhone.Enabled =
dtpBirth.Enabled =
dtpRegister.Enabled =
}
Enable All Controlprivate void EnableAllControl()true;true;true;true;true;true;true;true;true;
#endregion
{
{
MyCon.Close();
}
MyCon.ConnectionString =
MyCon.Open();
DisibleAllControl();
ShowAllCustomer();
FormatAllCustomer();
}
{
{
txtCode.Focus();
txtCode.SelectAll();
}
{
txtName.Focus();
txtName.SelectAll();
}
private void frmCustomer_Load(object sender, EventArgs e)if (MyCon.State == ConnectionState.Open)GlobVars.strConn;private void btAdd_Click(object sender, EventArgs e)bool IsFinish = false;if (txtCode.Text == "")MessageBox.Show("กรุณากรอกรหัสด้วย", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information);return;if (txtName.Text == "")MessageBox.Show("กรุณากรอกชื่อสกุลด้วย", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information);return;string tmpFileName = "";try
{
{
if (ImageFileName != "")//อ่านไฟล์ภาพเก็บไว้ที่ออบเจ็กต์ FS
FS =
new FileStream(@ImageFileName, FileMode.Open, FileAccess.Read);//กำหนดขนาดตัวแปรอาร์เรย์ CurrentImage
CurrentImage =
new byte[FS.Length];//
FS.Read(CurrentImage, 0,
FS.Close();
}
Convert.ToInt32(FS.Length));else
{
//กำหนดพาธของไฟล์ที่จะบันทึก
tmpFileName =
"C:\\tmp.jpg";//บันทึกรูปภาพที่เก็บไว้ในคุณสมบัติ ErrorImage
CustPic.ErrorImage.Save(tmpFileName);
//ให้เก็บรูปภาพไว้ที่ออบเจ็กต์ FS
FS =
new FileStream(tmpFileName, FileMode.Open, FileAccess.Read);//กำหนดขนาดตัวแปรอาร์เรย์ให้เท่ากับขนาดของไฟล์รูปภาพ
CurrentImage =
new byte[FS.Length];//เก็บรูปภาพไว้ที่ตัวแปรอาร์เรย์ CurrentImage
FS.Read(CurrentImage, 0,
Convert.ToInt32(FS.Length));//ปิดการใช้งานออบเจ็กต์ FS
FS.Close();
}
{
sqlInsert =
sqlInsert +=
sqlInsert +=
sqlInsert +=
sqlInsert +=
sqlInsert +=
sqlInsert +=
sqlInsert +=
sqlInsert +=
sqlInsert +=
sqlInsert +=
sqlInsert +=
MyCommAdd.CommandText = sqlInsert;
MyCommAdd.Connection = MyCon;
MyCommAdd.Parameters.Add(
MyCommAdd.Parameters.Add(
MyCommAdd.Parameters.Add(
MyCommAdd.Parameters.Add(
MyCommAdd.Parameters.Add(
MyCommAdd.Parameters.Add(
MyCommAdd.Parameters.Add(
MyCommAdd.Parameters.Add(
MyCommAdd.Parameters.Add(
MyCommAdd.Parameters.Add(
{
MyCon.Close();
}
MyCon.ConnectionString =
MyCon.Open();
MyCommAdd.CommandType =
MyCommAdd.CommandText = sqlInsert;
MyCommAdd.Connection = MyCon;
string sqlInsert;MySqlCommand MyCommAdd = new MySqlCommand();if (MessageBox.Show("ท่านต้องการเพิ่ม่ข้อมูลลูกค้าใหม่ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)"INSERT INTO tb_customer(customer_id,";"customer_code,";"customer_prefix,";"customer_name,";"customer_idcard,";"customer_regis,";"customer_education,";"customer_birth,";"customer_age,";"customer_phone,";"customer_pic)";"VALUES(NULL,?Code,?Prefix,?Name,?IDCard,?Register,?Education,?Birth,?Age,?Phone,?Picture)";"?Code", MySqlDbType.VarChar).Value = txtCode.Text;"?Prefix", MySqlDbType.VarChar).Value = cboPrefix.Text;"?Name", MySqlDbType.VarChar).Value = txtName.Text;"?IDCard", MySqlDbType.VarChar).Value = txtIDCode.Text;"?Register", MySqlDbType.Date).Value = dtpRegister.Value.ToShortDateString();"?Education", MySqlDbType.VarChar).Value = txtEducation.Text;"?birth", MySqlDbType.Date).Value = dtpBirth.Value.ToShortDateString();"?Age", MySqlDbType.Int16).Value = txtAge.Text;"?Phone", MySqlDbType.VarChar).Value = txtPhone.Text;"?Picture", MySqlDbType.LongBlob).Value = CurrentImage;if (MyCon.State == ConnectionState.Open)GlobVars.strConn;CommandType.Text;try
{
MyCommAdd.ExecuteNonQuery();
IsFinish =
}
{
IsFinish =
}
true;catch (MySqlException Myex)false;MessageBox.Show(Myex.ToString());finally
{
{
}
if (IsFinish == true)MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);else
{
}
}
ClearAllData();
ShowAllCustomer();
FormatAllCustomer();
}
}
MessageBox.Show("ไม่สามารถเพิ่มข้อมูลได้");catch
{
}
}
{
{
e.CellStyle.Format =
}
{
e.CellStyle.Format =
}
}
{
ofDlg.Title =
ofDlg.Filter =
ofDlg.FileName =
ofDlg.Multiselect =
ofDlg.FilterIndex = 0;
ofDlg.ShowDialog();
ImageFileName = ofDlg.FileName;
{
CustPic.SizeMode =
CustPic.Image =
}
private void DGAllCustomer_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)if (DGAllCustomer.Columns[e.ColumnIndex].Name == "customer_regis")"D";if (DGAllCustomer.Columns[e.ColumnIndex].Name == "customer_birth")"D";private void CustPic_Click(object sender, EventArgs e)"กรุณาเลือกไฟล์ภาพ";"JPEG(*.jpg)|*.jpg";"";false;if (ofDlg.FileName != "")PictureBoxSizeMode.StretchImage;Image.FromFile(ImageFileName);else
{
ImageFileName =
CustPic.SizeMode =
CustPic.Image = CustPic.ErrorImage;
}
}
{
ImageFileName =
CustPic.SizeMode =
CustPic.Image = CustPic.ErrorImage;
}
{
CurrentCustomer = DGAllCustomer.Rows[e.RowIndex].Cells[0].Value.ToString();
txtCode.Text = DGAllCustomer.Rows[e.RowIndex].Cells[0].Value.ToString();
cboPrefix.Text = DGAllCustomer.Rows[e.RowIndex].Cells[1].Value.ToString();
txtName.Text = DGAllCustomer.Rows[e.RowIndex].Cells[2].Value.ToString();
txtIDCode.Text = DGAllCustomer.Rows[e.RowIndex].Cells[3].Value.ToString();
dtpRegister.Value =
txtEducation.Text = DGAllCustomer.Rows[e.RowIndex].Cells[5].Value.ToString();
dtpBirth.Value =
txtAge.Text = DGAllCustomer.Rows[e.RowIndex].Cells[7].Value.ToString();
txtPhone.Text = DGAllCustomer.Rows[e.RowIndex].Cells[8].Value.ToString();
ShowImage();
}
{
{
txtCode.Focus();
txtCode.SelectAll();
}
{
txtName.Focus();
txtName.SelectAll();
}
sqlEdit =
sqlEdit +=
sqlEdit +=
sqlEdit +=
sqlEdit +=
sqlEdit +=
sqlEdit +=
sqlEdit +=
sqlEdit +=
sqlEdit +=
sqlEdit +=
Mycomm.CommandText = sqlEdit;
Mycomm.CommandType =
Mycomm.Connection = MyCon;
Mycomm.Parameters.Clear();
Mycomm.Parameters.Add(
Mycomm.Parameters.Add(
Mycomm.Parameters.Add(
Mycomm.Parameters.Add(
Mycomm.Parameters.Add(
Mycomm.Parameters.Add(
Mycomm.Parameters.Add(
Mycomm.Parameters.Add(
Mycomm.Parameters.Add(
{
MyCon.Close();
}
MyCon.ConnectionString =
MyCon.Open();
{
"";PictureBoxSizeMode.StretchImage;private void btDelPic_Click(object sender, EventArgs e)"";PictureBoxSizeMode.StretchImage;private void DGAllCustomer_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)Convert.ToDateTime(DGAllCustomer.Rows[e.RowIndex].Cells[4].Value.ToString());Convert.ToDateTime(DGAllCustomer.Rows[e.RowIndex].Cells[6].Value.ToString());private void btEdit_Click(object sender, EventArgs e)bool isComplete = false;if (txtCode.Text == "")MessageBox.Show("กรุณากรอกรหัสด้วย", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information);return;if (txtName.Text == "")MessageBox.Show("กรุณากรอกชื่อสกุลด้วย", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information);return;string sqlEdit;MySqlCommand Mycomm = new MySqlCommand();"UPDATE tb_customer";" SET customer_prefix=?Prefix,";"customer_name=?Name,";"customer_idcard=?IDCard,";"customer_regis=?DateRegis,";"customer_education=?Education,";"customer_birth=?Birth,";"customer_age=?Age,";"customer_phone=?Phone,";"customer_pic=?Picture";" WHERE customer_code=?Code";CommandType.Text;"?Prefix", MySqlDbType.VarChar).Value = cboPrefix.Text;"?Name", MySqlDbType.VarChar).Value = txtName.Text;"?IDCard", MySqlDbType.VarChar).Value = txtIDCode.Text;"?DateRegis", MySqlDbType.Date).Value = dtpRegister.Value.ToShortDateString();"?Education", MySqlDbType.VarChar).Value = txtEducation.Text;"?Birth", MySqlDbType.Date).Value = dtpBirth.Value.ToShortDateString();"?Age", MySqlDbType.Int16).Value = txtAge.Text;"?Phone", MySqlDbType.VarChar).Value = txtPhone.Text;"?Code", MySqlDbType.VarChar).Value = txtCode.Text;if (MyCon.State == ConnectionState.Open)GlobVars.strConn;if (MessageBox.Show("ท่านต้องการแก้ไขข้อมูล ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)try
{
Mycomm.ExecuteNonQuery();
isComplete =
}
{
isComplete =
}
true;catch (MySqlException ex)MessageBox.Show(ex.ToString());false;finally
{
{
}
if (isComplete == true)MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว", "ผลการทำงาน");else
{
}
}
ClearAllData();
ShowAllCustomer();
FormatAllCustomer();
}
}
{
{
txtCode.Focus();
txtCode.SelectAll();
}
sqlDel =
Mycomm.CommandText = sqlDel;
Mycomm.CommandType =
Mycomm.Connection = MyCon;
Mycomm.Parameters.Clear();
Mycomm.Parameters.Add(
{
MyCon.Close();
}
MyCon.ConnectionString =
MyCon.Open();
{
MessageBox.Show("ไม่สามารถแก้ไขข้อมูลได้", "เกิดผิดพลาด");private void btDel_Click(object sender, EventArgs e)string sqlDel;bool isComplete = false;if (txtCode.Text == "")MessageBox.Show("ไม่มีรหัสใช้ในการลบ", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information);return;MySqlCommand Mycomm = new MySqlCommand();"DELETE FROM tb_customer WHERE customer_code=?Customer_code";CommandType.Text;"?Customer_code", MySqlDbType.VarChar).Value = txtCode.Text;if (MyCon.State == ConnectionState.Open)GlobVars.strConn;if (MessageBox.Show("ท่านต้องการลบ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)try
{
Mycomm.ExecuteNonQuery();
isComplete =
}
{
isComplete =
}
true;catch (MySqlException ex)MessageBox.Show(ex.ToString());false;finally
{
{
}
if (isComplete == true)MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว","ผลการทำงาน");else
{
}
}
}
ClearAllData();
ShowAllCustomer();
FormatAllCustomer();
}
{
EnableAllControl();
ClearAllData();
}
}
}
MessageBox.Show("ไม่สามารถลบข้อมูลได้","เกิดข้อผิดพลาด");private void btNew_Click(object sender, EventArgs e)
using