【发布时间】:2013-06-26 22:13:33
【问题描述】:
我正在尝试将图片保存到我的 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();
这就是我将值传递给数据库的方式:
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;
不发送图片,但是数组有图片...
我做错了什么? 谢谢
【问题讨论】:
-
我知道,但它是为一个项目提供数据库的,我必须按原样使用它。所以我必须保持图像类型.. :( 但正如我所说的问题是我如何将我的字节数组的值传递给行 [“图片”],知道这个列是 sql 图像类型......
-
这是围绕连接的完整代码吗?连接和适配器调用是否都包含在 using 语句中?
-
@DanSnell @marc_s 这几乎是完整的代码,一切正常,我的项目几乎完成了,除了唯一不工作的是我的包含图片的字节数组如何进入@ 987654336@ 这是 sql 图像类型.....我错过了什么吗?或者我需要转换它还是什么? :(
标签: c# sql image sql-server-2008 filestream