Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将图像从C#保存到SQL Server数据库_C#_Sql_Image_Sql Server 2008_Filestream - Fatal编程技术网

将图像从C#保存到SQL Server数据库

将图像从C#保存到SQL Server数据库,c#,sql,image,sql-server-2008,filestream,C#,Sql,Image,Sql Server 2008,Filestream,我正在尝试将图片保存到SQL Server数据库中,但它不起作用 myCustomers表中的Picture列属于image类型,我正在尝试将带有图片的字节数组传递给Picture列 我的代码是这样的: SqlConnection conn = new SqlConnection("my working connection string"); SqlDataAdapter adapter = new SqlDataAdapter("Select * from Customers", conn)

我正在尝试将图片保存到SQL Server数据库中,但它不起作用

my
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;
它不发送图片,但数组有图片

我做错了什么?
谢谢

您是否使用对
数据集的更改来更新数据库

SqlConnection conn = new SqlConnection("my working connection string");
SqlDataAdapter adapter = new SqlDataAdapter("Select * from Customers", conn);

DataSet ds = new DataSet();

// Create command builder. This line automatically generates the update commands for you, so you don't 
// have to provide or create your own.
SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(adapter);

// Set the MissingSchemaAction property to AddWithKey because Fill will not cause primary
// key & unique key information to be retrieved unless AddWithKey is specified.
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

adapter.Fill(ds, "Customers");

// .... Code to fill the picArray....

DataRow row = ds.Tables[0].NewRow();
row["Id"] = "ID";
row["Name"] = "NAME";
row["Picture"] = picArray;
ds.Tables["Customers"].Rows.Add(row);

adapter.Update(ds, "Customers"); //Update the changes to the database

我纠正了这个问题!:D

@DanSnell说的话让我想到我做错了什么

由于连接和适配器的代码很好,我发现没有传递包含图像的字节数组参数,而是传递了一个
null字节数组

这就是为什么我将字节数组添加到新行
row[“Picture”]=picArray,列图片没有收到任何值,只是一个空值

纠正了将字节数组传递到类内部的方法,解决了问题。


我没有更改上面的任何代码,因此它可能会帮助希望将图像从C#保存到SQL Server数据库的人。。因为行[“Id”]和行[“Name”]可以工作。这两行已更新到数据库,但“row[“Picture”]=picArray;”未更新。因此,“Picture”列在数据库中保留空值。我也有SqlCommandBuilder,我忘了提到我知道,但它是为一个项目提供的数据库,我必须按原样使用它。因此,我必须保持图像类型..:(但正如我所说的,问题是如何将字节数组的值传递到行[“Picture”],知道此列是sql映像类型…这是连接的完整代码吗?连接和适配器调用是否都包含在using语句中?@DanSnell@marc_s这几乎是完整的代码,一切都正常,我的项目几乎完成了,唯一不正常的是我包含图片进入
行[“图片”]
,这是sql图像类型…..我是否缺少什么?或者我需要转换它还是什么(