如何将两个不同的图像插入到表? [英] how to insert two different images to a table?
问题描述
下面的代码用于将2张不同的图像插入到一张表中,但是它添加了同一张图片(即使选择了两张不同的图片),如何避免这种情况呢?有什么建议吗?
请帮忙!!!
following code is used to insert 2 different images to a table.But it add the same picture (even though two different pictures are selected).how can we avoid this?any suggestions?
pls help!!!
openConnection();
string qry = "insert into candidates ([NIC],[cand_no],[cand_name],[party(name)],[party_im],[name_im]) values (@nic,@no,@nm,@pty,@ImageData1,@ImageData2)";
SqlCom = new SqlCommand(qry, conn);
SqlCom.Parameters.Add(new SqlParameter("@nic", textBox1.Text));
SqlCom.Parameters.Add(new SqlParameter("@no", textBox2.Text));
SqlCom.Parameters.Add(new SqlParameter("@nm", textBox3.Text));
SqlCom.Parameters.Add(new SqlParameter("@pty", textBox5.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", (object)imagedata));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata));
SqlCom.ExecuteNonQuery();
closeConnection();
MessageBox.Show("Succesfully Inserted");
candidates table
NIC varchar(50)
cand_no int
cand_name nvarchar(50)
[party(name)] varchar(50)
party_im image
name_im image
对于party_im& name_im字段的图片是从OpenFileDialog中获取的,并将所选图片放入pictureBox1和pictureBox2
如何将这2张不同的图片分别添加到相关字段中?
请帮助我!!
for party_im & name_im fields pictures are taken from a OpenFileDialog, and selected pictures are put in to pictureBox1 and pictureBox2
how can we add these 2 different pictures separately to relevant fields?
pls help me!!!
推荐答案
不花更多时间捕捉代码就很难说出问题所在.
我有一个问题,什么是图像数据,以及用于将图像存储在数据库中的格式是什么类型.
我假设您正在将BLOB格式的值分配给imagedata以将图像存储在数据库中.
如果是,则分配两个不同的值,分别名为imagedata1和imagdata2,这两个值将分别以不同的方式存储byte[]
值.然后使用给定的代码
Its not easy to say what''s going wrong without taking more snaps of your code.
I''ve some question what is imagedata and what type of formating you are using to store image in database.
As I am assuming you are assigning BLOB formated values to imagedata to store images in database.
if yes then assign two different value named imagedata1 and imagdata2 which will storebyte[]
value differently for each. and then use given code
byte[] imagedata1 = getimagedata as byte formated for image1
byte[] imagedata2 = getimagedata as byte formated for image2
然后使用
and then use
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", (object)imagedata1)); // Change imagedata to imagedata1
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata2)); // Change imagedata to imagedata2
您必须使用两个不同的变量来保存选定的图像.
You must use two different variable to hold the selected images.
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", (object)imagedata));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata));
必须是
must be
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", (object)imagedata1));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata2));
其余代码对我有效.
The rest of the code is valid to me.
这篇关于如何将两个不同的图像插入到表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!