将图像插入DataBase错误“@Image'附近的语法不正确。”

时间:2013-01-21 18:29:32

标签: asp.net sql-server database image insert

我正在尝试将图片上传到我的数据库,但是我一直收到此错误

  

'@Image'附近的语法不正确

有什么建议吗?

 if (fileuploadImage.PostedFile != null &&
        fileuploadImage.PostedFile.FileName != "")
    {
        //getting Name of Uploaded File
        string strImageName = Profile.FirstName + Profile.LastName + "ProfileImage".ToString();

         //getting length of uploaded file
        int length = fileuploadImage.PostedFile.ContentLength;

        //create a byte array to store the binary image data
        byte[] imageSize = new byte[length];

        //store the currently selected file in memeory
        HttpPostedFile uploadedImage = fileuploadImage.PostedFile;

        //set the binary data
        uploadedImage.InputStream.Read(imageSize,0,length);


        string connectionString =

        ConfigurationManager.ConnectionStrings["NaviConnectionString"].ConnectionString;

        string insertSql = "INSERT INTO UserProfiles(ImageName,Image) VALUES (@ImageName,@Image";

        using (SqlConnection myConnection = new SqlConnection(connectionString))
        {

            myConnection.Open();

            SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
            myCommand.Parameters.AddWithValue("@ImageName", strImageName.ToString());
            myCommand.Parameters.AddWithValue("@Image", imageSize);
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }

    }

我也尝试过替换:

 myCommand.Parameters.AddWithValue("@Image", imageSize);

使用:

 myCommand.Parameters.AddWithValue("@Image", fileuploadImage.FileBytes);

但我仍然得到同样的错误!

1 个答案:

答案 0 :(得分:8)

你需要一个结束括号:

string insertSql = "INSERT INTO UserProfiles(ImageName,Image) VALUES (@ImageName,@Image";

应该是

string insertSql = "INSERT INTO UserProfiles(ImageName,Image) VALUES (@ImageName,@Image)";