2013-01-21 47 views
-1

我嘗試將圖片上傳到我的數據庫,但是我不斷收到此錯誤將圖像插入數據庫錯誤「@Image附近的語法錯誤」。

附近有語法錯誤@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

調試101:打印出您的SQL語句,並嘗試直接在SQL Server中執行它。即使沒有圖像,這個查詢也會失敗,因爲@iandayman指出了一個非常基本的語法錯誤。 –

回答

8

你需要一個右括號:

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

應該

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

謝謝,我得到了一切工作:) – FBEvo1