[英]Error : Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query
This is my program for uploading an image: 这是我上传图像的程序:
protected void Button1_Click(object sender, EventArgs e)
{
string car_model = TextBox1.Text;
string car_brand = TextBox2.Text;
string car_type = TextBox3.Text;
int dist_travel = int.Parse(TextBox4.Text);
int mileage = int.Parse(TextBox5.Text);
string car_condition = TextBox6.Text;
string car_owner = TextBox7.Text;
string car_color = TextBox8.Text;
int car_price = int.Parse(TextBox9.Text);
if (FileUpload1.HasFile)
{
byte[] fdata = FileUpload1.FileBytes;
string con_string = @"data source=(local);initial catalog=Automobile;integrated security=true";
SqlConnection con = new SqlConnection();
con.ConnectionString = con_string;
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Insert into vehicle_details values ('" + car_model + "','" + car_type + "','" + dist_travel + "','" + mileage + "','" + car_condition + "','" + car_owner + "','" + car_color + "','" + fdata + "','" + car_price + "')";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
then it throws the following error: 然后会引发以下错误:
Implicit conversion from data type
varchar
tovarbinary(max)
is not allowed.不允许从数据类型
varchar
隐式转换为varbinary(max)
。 Use theCONVERT
function to run this query.使用
CONVERT
函数运行此查询。
And structure of database is car_model varchar, car_type varchar, dist_travel int, mileage int,car_condition varchar, car_owner varchar, car_color varchar, fdata varbinary(MAX), car_price varchar 数据库的结构为car_model varchar,car_type varchar,dist_travel int,mileage int,car_condition varchar,car_owner varchar,car_color varchar,fdata varbinary(MAX),car_price varchar
You may have declared fdata as byte[] but the section of code that reads 您可能已经将fdata声明为byte [],但是读取的代码部分
... + "','" + fdata + "','" + ....
is forcing it into a string so that it can be part of the command text 将其强制为字符串,以便可以成为命令文本的一部分
You're going to have to parameterise the query 您将必须参数化查询
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.