这是我上传图片的程序:
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();
}
}
然后它会抛出以下错误:
不允许从数据类型
varchar
隐式转换为varbinary(max)
。使用CONVERT
函数运行此查询。
数据库的结构是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
答案 0 :(得分:1)
您可能已将fdata声明为byte [],但代码部分为
... + "','" + fdata + "','" + ....
将其强制转换为字符串,以便它可以成为命令文本的一部分
您将不得不参数化查询