2011-06-13 67 views
1

我試圖存儲與現在的問題是越來越存入兩行數據類型image SQL服務器映像,我使用FileUpload控件上傳圖片,我的代碼如下問題在SQL Server中存儲圖像

byte[] imagedata = ImageUpload.FileBytes; 
con.Open(); 
SqlCommand insertImageCmd = new SqlCommand("insert into Images(ImageName,Image) values (@name, @image)", con); 
insertImageCmd.Parameters.AddWithValue("@name", imageNameTextBox.Text); 
insertImageCmd.Parameters.AddWithValue("@image", imagedata); 
insertImageCmd.ExecuteNonQuery(); 
con.Close(); 

這是正確的方式存儲圖像?請幫忙!

回答

2

如果它獲得存儲爲兩行,用你已經發布的代碼,我認爲你可能以某種方式將您的網頁張貼雙倍。

1

這裏是我會做什麼:

byte[] image = File.ReadAllBytes(path) 

然後你就可以在你的數據庫插入。

提示:

當您使用一個SqlConnection,與using關鍵詞圍繞着它:

using (var connection = new SqlConnection(connectionString)) 
{ 

} 
+3

SqlCommand實例也應該位於using塊中。 – Andy 2011-06-13 17:01:37