|
 |
|
ต้องการบันทึกรูปลง Oleobject ของ MS Access โดยแปลงเป็น binary ก่อน |
|
 |
|
|
 |
 |
|
Code (C#)
/* โค้ดนี้อยู่ในปุ่มเซฟเพื่อเรียกใช้ทั้งสองเมธอ*/
saveData(convertImageToByte(pictureBox1.Image));
/*//////////////////////////////////////////////////////////////*/
private byte[] convertImageToByte(Image inputImage)
{
Bitmap bitmap = new Bitmap(inputImage);
MemoryStream memstream = new MemoryStream();
bitmap.Save(memstream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageByte = memstream.ToArray();
return imageByte;
}
public void saveData(byte[] imageByte)
{
conn.Open();
try
{
OleDbCommand command = new OleDbCommand("insert into People (HomeID, Name, CitizenID, Brithdate, Nationality, Country, Religion, Phone, MobilePhone, E-mail, MeterWaterNumber, MeterPowerNumber, CarNumber, HomePic, CarPic) " +
"values ('" + number + "','" + name + "','" + id + "','" + brithdate + "','" + nationality + "','" + country + "','" + religion + "','" + phone + "','" +
mobile + "','" + email + "','" + meterwater + "','" + meterpower + "','" + carnumber + "','" + "',@HomePic, @CarPic'", conn);
OleDbParameter imageparameter = command.Parameters.AddWithValue("@HomePic", SqlDbType.Binary);
imageparameter.Value = imageByte;
imageparameter.Size = imageByte.Length;
/*OleDbParameter imageparameter2 = command.Parameters.AddWithValue("@CarPic", SqlDbType.Binary);
imageparameter2.Value = imageByte;
imageparameter2.Size = imageByte.Length;*/
command.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show("" + e);
}
finally
{
conn.Close();
}
}
จากโค้ดนะครับ เมธอด แรกทำการแปลงรูปเป็นไบนารี่ โดยการรับรูปจาก picturebox1
แล้วเมธอคที่สองจะเป็นการเก็บลงฐานข้อมูล
คืออยากจะถามว่าผมต้องการจะส่งพารามิเตอร์สองตัวจาก picturebox 2 ตัว (คือจะเก็บสองรูปพร้อมกันนั่นเอง) ต้องทำยังไงครับ
และรีเทิร์นค่า 2 ตัวยังไง
ในเมธอดที่สอง ตรงคอมเม้นท์ ผมทำถูกหรือยังครับ ในกรณี 2 รูป
ขอบคุณล่วงหน้าครับ
Tag : .NET, Ms Access, Win (Windows App), C#

|
|
 |
 |
 |
 |
Date :
2014-07-27 01:58:15 |
By :
ideasza |
View :
1052 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code
private byte[] convertImageToByte(Image inputImage)
{
Bitmap bitmap = new Bitmap(inputImage);
MemoryStream memstream = new MemoryStream();
bitmap.Save(memstream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageByte = memstream.ToArray();
return imageByte;
}
conn.Open();
try
{
OleDbCommand command = new OleDbCommand("insert into People (HomeID, Name, CitizenID, Brithdate, Nationality, Country, Religion, Phone, MobilePhone, E-mail, MeterWaterNumber, MeterPowerNumber, CarNumber, HomePic, CarPic) " +
"values ('" + number + "','" + name + "','" + id + "','" + brithdate + "','" + nationality + "','" + country + "','" + religion + "','" + phone + "','" +
mobile + "','" + email + "','" + meterwater + "','" + meterpower + "','" + carnumber + "','" + "',@HomePic, @CarPic'", conn);
OleDbParameter imageparameter = command.Parameters.AddWithValue("@HomePic", SqlDbType.Binary);
imageparameter.Value = convertImageToByte(pictureBox1.Image);
imageparameter.Size = imageByte.Length;
/*OleDbParameter imageparameter2 = command.Parameters.AddWithValue("@CarPic", SqlDbType.Binary);
imageparameter2.Value = convertImageToByte(pictureBox1.Image);
imageparameter2.Size = imageByte.Length;*/
command.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show("" + e);
}
finally
{
conn.Close();
}
|
 |
 |
 |
 |
Date :
2014-07-27 07:48:39 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อีกวิธีสร้างเป็น Class Get/Set ครับ
Code
public class getImageByte
{
public byte[] ImgByte { get; set; }
public double ImgSize { get; set; }
}
private getImageByte convertImageToByte(Image inputImage)
{
getImageByte img = new getImageByte();
Bitmap bitmap = new Bitmap(inputImage);
MemoryStream memstream = new MemoryStream();
bitmap.Save(memstream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageByte = memstream.ToArray();
img.ImgByte = imageByte;
img.ImgSize = imageByte.Length;
return img;
}
conn.Open();
try
{
OleDbCommand command = new OleDbCommand("insert into People (HomeID, Name, CitizenID, Brithdate, Nationality, Country, Religion, Phone, MobilePhone, E-mail, MeterWaterNumber, MeterPowerNumber, CarNumber, HomePic, CarPic) " +
"values ('" + number + "','" + name + "','" + id + "','" + brithdate + "','" + nationality + "','" + country + "','" + religion + "','" + phone + "','" +
mobile + "','" + email + "','" + meterwater + "','" + meterpower + "','" + carnumber + "','" + "',@HomePic, @CarPic'", conn);
getImageByte img1 = new convertImageToByte(pictureBox1.Image);
OleDbParameter imageparameter = command.Parameters.AddWithValue("@HomePic", SqlDbType.Binary);
imageparameter.Value = img1.ImgByte;
imageparameter.Size = img1.ImgSize;
command.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show("" + e);
}
finally
{
conn.Close();
}
|
 |
 |
 |
 |
Date :
2014-07-27 07:53:38 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

Code (C#)
public void saveData()
{
try
{
valueText();
conn.Open();
OleDbCommand command = new OleDbCommand("People", conn);
command.Connection = conn;
command.CommandType = CommandType.Text;
command.CommandText = "insert into People (HomeID, Name, CitizenID, Brithdate, Nationality, Country, Religion, Phone, MobilePhone, E-mail, MeterWaterNumber, MeterPowerNumber, CarNumber) " +
"values ('" + number + "','" + name + "','" + id + "','" + brithdate + "','" + nationality + "','" + country + "','" + religion + "','" + phone + "','" +
mobile + "','" + email + "','" + meterwater + "','" + meterpower + "','" + carnumber + "')";
command.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show("" + e);
}
finally
{
conn.Close();
}
}
คือจะพยายามทดสอบ insert แต่ text ก่อนอ่ะครับ แต่ก็ติดตามรูปบนครับ
ขอคำแนะนำด้วยครับ
|
 |
 |
 |
 |
Date :
2014-07-27 14:37:02 |
By :
ideasza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|