不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。 [英] Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

查看:337
本文介绍了不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击提交按钮时出现错误。请帮助我。



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



I'm getting error when I click submit button. Please help me.

This is the Error: Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

protected void Button1_Click(object sender, EventArgs e)
        {
            //upload image

             if (FileUpload1.HasFile)
            {
              
                string str = FileUpload1.FileName;
                FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//images//" + str);
                string path = "~//images//";
                cn.Open();

                SqlCommand cmd = new SqlCommand("insert into tblStudent(std_ID,std_Name,std_Password,std_ContactNumber,std_Address,std_Image,module_ID,intake_Code) Values('" + TextBox2.Text + "','" +TextBox1.Text+"','"+TextBox3.Text+ "','"
                    +TextBox4.Text+"','" +TextBox5.Text+"','"+ path + "','" +CheckBoxList1+"','" +DropDownList1+"')", cn);
                cmd.Parameters.AddWithValue("@std_ID", TextBox2.Text);
                cmd.Parameters.AddWithValue("@std_Name", TextBox1.Text);
                cmd.Parameters.AddWithValue("@std_Password", TextBox3.Text);
                cmd.Parameters.AddWithValue("@std_ContactNumber", TextBox4.Text);
                cmd.Parameters.AddWithValue("@std_Address", TextBox5.Text);
                cmd.Parameters.AddWithValue("@std_Image", path);
                cmd.Parameters.AddWithValue("@module_ID", CheckBoxList1.SelectedValue);
                cmd.Parameters.AddWithValue("@intake_Code", DropDownList1.SelectedValue); 


                cmd.ExecuteNonQuery();
                cn.Close();

              System.Windows.Forms.MessageBox.Show("New Account Register Successfully!");
               
       
       
               

            }
            else
            {
                Label3.Text = "Please upload a image";
            }
            

        }

推荐答案

使用参数化语句会遇到很多问题离开。

但是如果你认为你在数据库中存储图像,你将不仅要传递文件的名称。



Use a parameterized statement and many problems will go away.
But if you think you are storing an image in the database, you will have to do more than pass the name of the file.

SqlCommand cmd = new SqlCommand("insert into tblStudent(std_ID,std_Name,std_Password,std_ContactNumber,std_Address,std_Image,module_ID,intake_Code) Values(@std_ID,@std_Name,@std_Password,@std_ContactNumber,@std_Address,@std_Image,@module_ID,@intake_Code)", cn);

cmd.Parameters.AddWithVlaue ( "@std_ID" , TextBox2.Text  ) ;
// repeat for the other values

cmd.ExecuteNonQuery();


使用参数化的sql语句,您还需要读取文件内容并将其设置为参数值。示例代码:

use parameterized sql statement and also you need to read the file content and set it as parameter value. sample code :
protected void Upload(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string contentType = FileUpload1.PostedFile.ContentType;
    using (Stream fs = FileUpload1.PostedFile.InputStream)
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "insert into tblFiles values (@Name, @ContentType, @Data)";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@Name", filename);
                    cmd.Parameters.AddWithValue("@ContentType", contentType);
                    cmd.Parameters.AddWithValue("@Data", bytes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}



参考:从ASP.Net中的SQL Server数据库上传和下载文件 [ ^ ]


更改了数据类型成像。


这篇关于不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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