PictureBox ต้องเขียนยังไงครับ วานผู้รู้ตอบด้วยครับ แล้วให้มั้นดึง ID มาใส่ให้ด้วย นามสกุลไฟล์ด้วยครับ
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.SqlTypes;
using System.Data.SqlClient;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.IO;
namespace man
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection Conn;
SqlCommand com = new SqlCommand();
SqlTransaction tr;
SqlDataReader dr;
StringBuilder sb = new StringBuilder();
string strConn;
private void Form1_Load(object sender, EventArgs e)
{
strConn = "Data Source=.\\SQLExpress;Initial Catalog=Man;Integrated Security=True";
Conn = new SqlConnection();
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
sb = new StringBuilder();
sb.Append("SELECT ProvinceID,ProvinceName FROM Province;");
string sqlIni = sb.ToString();
com = new SqlCommand();
com.CommandText = sqlIni;
com.CommandType = CommandType.Text;
com.Connection = Conn;
dr = com.ExecuteReader();
if (dr.HasRows)
{
DataTable dtProvince = new DataTable();
dtProvince.Load(dr);
cboProvince.BeginUpdate();
cboProvince.DisplayMember = "ProvinceName";
cboProvince.ValueMember = "ProvinceID";
cboProvince.DataSource = dtProvince;
cboProvince.EndUpdate();
}
}
private void crmok_Click(object sender, EventArgs e)
{
if (mskIDCard.Text.Trim() == "")
{
MessageBox.Show("กรุณาป้อนรหัสบัตรประชาชนก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
mskIDCard.Focus();
return;
}
if (txtAmphur.Text.Trim() == "")
{
MessageBox.Show("กรุณาป้อนชื่ออำเภอก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtAmphur.Focus();
return;
}
if (MessageBox.Show("คุณต้องการเพิ่มใหม่ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
{
tr = Conn.BeginTransaction();
try
{
sb.Remove(0, sb.Length);
sb.Append("INSERT INTO ManName (IDCard,AmphurCard,ProvinceCard,FirstCard,ExpireCard)");
sb.Append(" VALUES (@IDCard,@AmphurCard,@ProvinceCard,@FirstCard,@ExpireCard)");
string sqlAdd;
sqlAdd = sb.ToString();
com.CommandText = sqlAdd;
com.CommandType = CommandType.Text;
com.Connection = Conn;
com.Transaction = tr;
com.Parameters.Clear();
com.Parameters.Add("@IDCard", SqlDbType.NVarChar).Value = mskIDCard.Text.Trim();
com.Parameters.Add("@ProvinceCard", SqlDbType.NChar).Value = cboProvince.Text.Trim();
com.Parameters.Add("@AmphurCard", SqlDbType.NVarChar).Value = txtAmphur.Text.Trim();
com.Parameters.Add("@FirstCard", SqlDbType.DateTime).Value = dtpAdmitDate.Text.Trim();
com.Parameters.Add("@ExpireCard", SqlDbType.DateTime).Value = ExpireDate1.Text.Trim();
com.ExecuteNonQuery();
tr.Commit();
MessageBox.Show("เพิ่มข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("คุณป้อนรหัสบัตรประชาชนซ้ำ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
tr.Rollback();
}
}
mskIDCard.Focus();
}
private void picHumanPicture_Click(object sender, EventArgs e)
{
long image_length;
byte[] image_byte;
FileStream fs;
FileInfo fInfo;
OpenFileDialog of = new OpenFileDialog();
of.Filter = "Images (*.BMP;*.JPG;*.GIF) |*.BMP;*.JPG;*.GIF | All Files (*.*)| *.*";
of.Title = "Open Image Files";
try
{
if (of.ShowDialog() == DialogResult.OK)
{
picHumanPicture.Image = Image.FromFile(of.FileName);
fInfo = new FileInfo(of.FileName);
image_length = fInfo.Length;
fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
image_byte = new byte[Convert.ToInt32(image_length)];
int read = fs.Read(image_byte, 0, Convert.ToInt32(image_length));
fs.Close();
if (read == 0)
{
MessageBox.Show("Cannot Save image");
}
}
}
catch (Exception)
{
}
}
private void cmdDeletePicture_Click(object sender, EventArgs e)
{
picHumanPicture.Image = null;
}
}
}Tag : C#
Date :
2011-01-23 06:00:43
By :
sak
View :
959
Reply :
1
Load balance : Server 03