简体   繁体   English

错误:不允许从数据类型varchar到varbinary(max)的隐式转换。 使用CONVERT函数运行此查询

[英]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 to varbinary(max) is not allowed. 不允许从数据类型varchar隐式转换为varbinary(max) Use the CONVERT 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.

相关问题 不允许从数据类型sql_variant隐式转换为varbinary(max)。 使用CONVERT函数运行此查询。 ASP.NET SQL数据源 - Implicit conversion from data type sql_variant to varbinary(max) is not allowed. Use the CONVERT function to run this query. ASP.NET SQL DataSource 不允许从数据类型datetime到int的隐式转换错误。 使用CONVERT函数运行此查询 - Error of Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query 不允许从数据类型nvarchar到二进制的隐式转换。 使用CONVERT函数运行此查询。 - Implicit conversion from data type nvarchar to binary is not allowed. Use the CONVERT function to run this query. Dapper:不允许从数据类型 datetime 到 int 的隐式转换。 使用 CONVERT function 运行此查询 - Dapper: Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query 回滚迁移错误不允许从数据类型datetime到int的隐式转换。 使用CONVERT函数运行此查询 - Roll Back Migration Error Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query 如何处理异常不允许从数据类型varchar隐式转换为varbinary(max)。 在将数据添加到数据库时 - how to handle the exception Implicit conversion from data type varchar to varbinary(max) is not allowed. while adding the data to database 在数据库中存储字节时出错,不允许从数据类型varchar到varbinary(max)的隐式转换 - Error while storing bytes in database ,Implicit conversion from data type varchar to varbinary(max) is not allowed 无法在错误的varbinary(max)中插入空值:不允许从数据类型nvarchar隐式转换为varbinary(max) - Cannot insert null value in varbinary(max) with error : implicit conversion from data type nvarchar to varbinary(max) is not allowed 不允许从数据类型varchar隐式转换为varbinary(max)C# - Implicit conversion from data type varchar to varbinary(max) is not allowed c# 不允许从数据类型nvarchar隐式转换为varbinary(max) - Implicit conversion from data type nvarchar to varbinary(max) is not allowed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM