(无法将参数值从位图转换为字节[])将插入图像插入数据库时​​出错 [英] (Failed to convert parameter value from a Bitmap to a Byte[]) Error while inserting inserting images into database

查看:54
本文介绍了(无法将参数值从位图转换为字节[])将插入图像插入数据库时​​出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在该应用程序中创建了1个Windows应用程序我创建了包含图像字段的注册表单。填写该表单中的所有详细信息后当我点击保存按钮我是运行时错误(无法将参数值从位图转换为字节[])..任何人都可以告诉我如何解决这个问题..我试着从最近4天开始解决这个问题。我在很多论坛上发布了这个问题,但至少没有使用我来到字节论坛..我希望我能从这个论坛得到解决方案。





这是我在按钮方式下编写的代码::



Hi everyone,

I've created 1 windows app in that app i've created Registration form including image fields.After filling all the details in that form when i click on save button i'm runtime error(Failed to convert parameter value from a Bitmap to a Byte[])..Can anyone tell me how to resolve this issue..I'm try to resolve this from since last 4days.I posted the question on so many forums but no use atleast i came to bytes forum..I hope I'll get the solution from this forum.


This is the code which I've written under button method::

private void btnaddinfo_Click(object sender, EventArgs e)
{
string scn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;
using (SqlConnection cn = new SqlConnection(scn))
{
using (SqlCommand cmd = new SqlCommand("SP_Info", cn))
{
try
{
byte[] photo = GetPhoto(imageloc);

cmd.CommandType = CommandType.StoredProcedure;

SqlParameter p1 = new SqlParameter("@Hp_Number", SqlDbType.NVarChar, 50);
SqlParameter p2 = new SqlParameter("@Customer_Name", SqlDbType.VarChar, 50);
SqlParameter p3 = new SqlParameter("@Customer_Contact_Number", SqlDbType.NVarChar, 15);
SqlParameter p4 = new SqlParameter("@Guarantor_Name", SqlDbType.VarChar, 50);
SqlParameter p5 = new SqlParameter("@Guarantor_Contact_Number", SqlDbType.NVarChar, 15);
SqlParameter p6 = new SqlParameter("@Hp_Date", SqlDbType.Date);
SqlParameter p7 = new SqlParameter("@Customer_Address", SqlDbType.NVarChar, Max);
SqlParameter p8 = new SqlParameter("@Vehicle", SqlDbType.VarChar, 50);
SqlParameter p9 = new SqlParameter("@Vehicle_Model", SqlDbType.VarChar, 50);
SqlParameter p10 = new SqlParameter("@Vehicle_Number", SqlDbType.NVarChar, 50);
SqlParameter p11 = new SqlParameter("@Chasis_Number", SqlDbType.NVarChar, 50);
SqlParameter p12 = new SqlParameter("@Engine_Number", SqlDbType.NVarChar, 50);
SqlParameter p13 = new SqlParameter("@FC_Date", SqlDbType.Date);
SqlParameter p14 = new SqlParameter("@Insurance_Date", SqlDbType.Date);
SqlParameter p15 = new SqlParameter("@Insurance_Amount", SqlDbType.Int);
SqlParameter p16 = new SqlParameter("@Paid_Amount", SqlDbType.Int);
SqlParameter p17 = new SqlParameter("@Vehicle_Pic",SqlDbType.Image,photo. Length);
SqlParameter p18 = new SqlParameter("@Customer_Pic ", SqlDbType.Image, photo.Length);
SqlParameter p19 = new SqlParameter("@Guarantor_Pic ", SqlDbType.Image, photo.Length);
SqlParameter p20 = new SqlParameter("@Documents_Pic", SqlDbType.Image, photo.Length);
SqlParameter p21 = new SqlParameter("@Insurance_Pic", SqlDbType.Image, photo.Length);

cmd.Parameters.Add(p1).Value = tbhpnum.Text; 
cmd.Parameters.Add(p2).Value = tbcusnam.Text; 
cmd.Parameters.Add(p3).Value=tbcusmblno.Text;
cmd.Parameters.Add(p4).Value = tbguanam.Text;
cmd.Parameters.Add(p5).Value = tbguamblno.Text;
cmd.Parameters.Add(p6).Value = DateTime.Parse(tbhpdat.Text);
cmd.Parameters.Add(p7).Value = tbcusadd.Text;
cmd.Parameters.Add(p8).Value = tbveh.SelectedItem.ToString();
cmd.Parameters.Add(p9).Value = tbvehmod.SelectedItem.ToString();
cmd.Parameters.Add(p10).Value = tbvehnum.Text;
cmd.Parameters.Add(p11).Value = tbchanum.Text;
cmd.Parameters.Add(p12).Value = tbengnum.Text;
cmd.Parameters.Add(p13).Value = DateTime.Parse(tbfcdat.Text);
cmd.Parameters.Add(p14).Value = DateTime.Parse(tbinsdat.Text);
cmd.Parameters.Add(p15).Value = Convert.ToInt32(tbinsamt.Text);
cmd.Parameters.Add(p16).Value = Convert.ToInt32(tbpaiamt.Text);
cmd.Parameters.Add(p17).Value = boxvehpic;
cmd.Parameters.Add(p18).Value = boxcuspic;
cmd.Parameters.Add(p19).Value = boxguapic;
cmd.Parameters.Add(p20).Value=boxdocpic;
cmd.Parameters.Add(p21).Value = boxinspic;

if (cn.State != ConnectionState.Open)
cn.Open();
int count = cmd.ExecuteNonQuery();
if (count == 1)
{
MessageBox.Show(count.ToString() + "Record(s) Saved.");
}
else
{
MessageBox.Show("Please correct the error which you have entered and then click on addinformation button");
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());

}
finally
{
if (cn.State == ConnectionState.Open)
cn.Close();
}
}
}
}









外部字节[]方法代码::







External Byte[] method code::

public static byte[] GetPhoto(string imageloc)
{
byte[] photo= null;
FileStream stream = new FileStream(imageloc, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
photo= reader.ReadBytes((int)stream.Length);

reader.Close();
stream.Close();

return photo;
}





[ ^ ] &<>

存储过程::






[^]&<>
Stored Procedure::


create PROC SP_Info
(
@Hp_Number nvarchar(50),
@Customer_Name varchar(50),
@Customer_Contact_Number nvarchar(15),
@Guarantor_Name varchar(50),
@Guarantor_Contact_Number nvarchar(15),
@Hp_Date date,
@Customer_Address nvarchar(MAX),
@Vehicle varchar(50),
@Vehicle_Model varchar(50),
@Vehicle_Number nvarchar(50),
@Chasis_Number nvarchar(50),
@Engine_Number nvarchar(50),
@FC_Date date,
@Insurance_Date date,
@Insurance_Amount int,
@Paid_Amount int,
@Vehicle_Pic image,
@Customer_Pic image,
@Guarantor_Pic image,
@Documents_Pic image,
@Insurance_Pic image
)
AS
BEGIN
Insert into Info values(@Hp_Number,
@Customer_Name,
@Customer_Contact_Number,
@Guarantor_Name,
@Guarantor_Contact_Number,
@Hp_Date,
@Customer_Address,
@Vehicle,
@Vehicle_Model,
@Vehicle_Number,
@Chasis_Number,
@Engine_Number,
@FC_Date,
@Insurance_Date,
@Insurance_Amount,
@Paid_Amount,
@Vehicle_Pic,
@Customer_Pic,
@Guarantor_Pic,
@Documents_Pic,
@Insurance_Pic)
END

推荐答案

嗯...你使用字节数组的唯一方法是得到它的长度:

Um...The only thing you use the array of bytes for is to get it's length:
byte[] photo = GetPhoto(imageloc);
... 
SqlParameter p17 = new SqlParameter("@Vehicle_Pic",SqlDbType.Image,photo. Length);
SqlParameter p18 = new SqlParameter("@Customer_Pic ", SqlDbType.Image, photo.Length);
SqlParameter p19 = new SqlParameter("@Guarantor_Pic ", SqlDbType.Image, photo.Length);
SqlParameter p20 = new SqlParameter("@Documents_Pic", SqlDbType.Image, photo.Length);
SqlParameter p21 = new SqlParameter("@Insurance_Pic", SqlDbType.Image, photo.Length);



整数值不能作为byte [] ...



另外,如果您尝试将相同的数据加载到多个图像,请将其作为一个参数发送,并多次​​传递 - 这样效率会更高,尤其是图像变得非常大......


And an integer value isn't going to work as a byte[]...

Plus, if you are trying to load the same data to several images, send it as one parameter, and pass that multiple times - it's a lot more efficient, especially when images get quite large...


这篇关于(无法将参数值从位图转换为字节[])将插入图像插入数据库时​​出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆