图像未保存在数据库中 [英] Image not saving in database
问题描述
void conv_photo()
{
//converting photo to binary data
if (pictureBox3.Image != null)
{
ms = new MemoryStream();
pictureBox3.Image.Save(ms, ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("@photo", photo_aray);
}
}
private void btnsave_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex >= 0 && textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && comboBox2.SelectedIndex >= 0 && textBox4.Text != "" && richTextBox1.Text != "")
{
try
{
//cmd = new SqlCommand("insert into student_details(class,reg_num,name,father,gender,dob,phone,adrs,img) values('" + comboBox1.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + comboBox2.Text + "','" + dateTimePicker1.Text + "','" + textBox4.Text + "','" + richTextBox1.Text + "',@photo)", con);
string cmdtext = "insert into student_details(class,reg_num,name,father,gender,dob,phone,adrs,img) values(@class,@reg_num,@name,@father,@gender,@dob,@phone,@adrs,@photo)";
SqlCommand cmd = new SqlCommand(cmdtext, con);
cmd.Parameters.AddWithValue("@class", comboBox1.Text);
cmd.Parameters.AddWithValue("@reg_num", textBox1.Text);
cmd.Parameters.AddWithValue("@name", textBox2.Text);
cmd.Parameters.AddWithValue("@father", textBox3.Text);
cmd.Parameters.AddWithValue("@gender", comboBox2.Text);
cmd.Parameters.AddWithValue("@dob", DateTime.Parse(dateTimePicker1.Text));
cmd.Parameters.AddWithValue("@phone", textBox4.Text);
cmd.Parameters.AddWithValue("@adrs", richTextBox1.Text);
conv_photo();
con.Open();
int n = cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
{
MessageBox.Show("record inserted");
load();
DisplaySummary();
student_form_clear();
getregnum();
}
else
MessageBox.Show("insertion failed, please select class and gender");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("Please fill all entries");
}
}
我尝试过:
我正在尝试此代码,但无法在数据库中保存图像。除了图像,所有数据都可以轻松保存。
错误:必须声明sclare变量@photo
我使用以下数据类型
img = image;
dob =日期;
和其他人的varchar。
哪里有问题请帮帮我
提前感谢
What I have tried:
I am trying this code but unable to save image in database. except image all data saves easily.
ERROR: must declare sclare variable @photo
I used following datatype
img = image;
dob = date;
and varchar for others.
where is problem please help me
thanks in advance
推荐答案
您创建一个新的SqlCommand
对象名为cmd
在btnsave_Click
方法中,作为局部变量。但是,当您再调用conv_photo
来保存图像时,代码中引用了一个不同的cmd
对象在代码中的其他地方声明。您应该将cmd
对象作为参数传递给conv_photo
,这样您就可以确定它正在寻找正确的对象。
You create a newSqlCommand
object namedcmd
inside yourbtnsave_Click
method, as a local variable. However, when you then callconv_photo
to save the image the code there is referring to a differentcmd
object that has been declared somewhere else in your code. You should pass thecmd
object as a parameter toconv_photo
so you are sure it is addressing the correct object.
这很简单:你有一个类级别的SqlCommand对象,名为cmd
,你要将图像作为参数添加到conv_photo
方法。但是......在你的btnsave_Click
方法中,你创建了一个同名的新局部变量:
It's pretty simple: you have a class level SqlCommand object calledcmd
to which you are adding the image as a parameter in yourconv_photo
method. But...in yourbtnsave_Click
method you create a new local variable of the same name:
SqlCommand cmd = new SqlCommand(cmdtext, con);
你用它来做INSERT操作。
因为他们不喜欢两者都使用相同的SqlCommand实例,@ this参数被添加到错误的SqlCommand并且SQL因此抱怨。
我?我将删除类级别版本,并将相应的SqlCommand对象传递给 conv_photo
方法,以确保始终将其添加到正确的实例。
你的转换不需要那么复杂:
Which you use to do teh INSERT operation.
Because they don't both use the same SqlCommand instance, the @photo parameter is added to the wrong SqlCommand and SQL complains as a result.
Me? I'd delete the class level version, and pass the appropriate SqlCommand object to the conv_photo
method to ensure you always add it to the correct instance.
And your conversion doesn't need to be so complex:
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] data = ms.ToArray();
将会这样做。
will do it.
这篇关于图像未保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!