我試圖將圖片保存到我的SQL Server數據庫中,但它不起作用。將C#中的圖像保存到SQL Server數據庫中
我Customers
表中的列Picture
是image
型的,我試圖跟圖片上的字節數組傳遞給Picture
列
我的代碼是這樣的:
SqlConnection conn = new SqlConnection("my working connection string");
SqlDataAdapter adapter = new SqlDataAdapter("Select * from Customers", conn);
DataSet ds = new DataSet("Customers");
adapter.Fill(ds);
然後我爲圖像創建一個字節數組:
string path = null;
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog();
path = fileDialog.FileName;
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] picArray = new byte[fs.Length];
fs.Read(picArray, 0, Convert.ToInt32(fs.Length));
fs.Close();
這是我如何將值傳遞給d atabase:
DataRow row = ds.Tables[0].NewRow();
row["Id"] = "ID";
row["Name"] = "NAME";
row["Picture"] = picArray;
ds.Tables[0].Rows.Add(row);
adapter.Update(ds);
的問題是這一行:
row["Picture"] = picArray;
它不發送圖片,但陣中擁有的圖片...
我在做什麼錯? 謝謝
[*'ntext','text'和'圖像數據類型將在未來版本的SQL Server中刪除。避免在新的開發工作中使用這些數據類型,並計劃修改當前正在使用它們的應用程序。使用'nvarchar(max)','varchar(max)'和'varbinary(max)'代替。*](http://msdn.microsoft.com/en-us/library/ms187993.aspx) –
我知道,但是這是一個給數據庫的項目,我必須照原樣使用它。所以我必須保持與圖像類型.. :(但正如我所說的問題是我如何將字節數組的值傳遞給行[「圖片」],知道此列是sql圖像類型... – user1358261
這是連接的完整代碼嗎?連接和適配器調用是否都使用語句包裝? –