使用 Visual C# 将图像添加到 SQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19817552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding an image to SQL database using Visual C#
提问by Jeena
I am working on a visual C# program for image processing.I am trying to add image to sql database using Visual C# (Windows forms) and ADO.NET.
我正在开发一个用于图像处理的可视化 C# 程序。我正在尝试使用 Visual C#(Windows 窗体)和 ADO.NET 将图像添加到 sql 数据库。
I have converted the image to binary form with filestream method but the image bytes are not getting saved in database. At the database image column, it says < Binary data >and no data is getting saved!
我已使用文件流方法将图像转换为二进制形式,但图像字节未保存在数据库中。在数据库图像列,它显示 < 二进制数据 >并且没有数据被保存!
I have tried many methods for inserting( with and without stored procedures..etc) but always getting the same thing at database.
我尝试了很多插入方法(有和没有存储过程......等),但总是在数据库中得到同样的东西。
private void button6_Click(object sender, EventArgs e)
{
try
{
byte[] image = null;
pictureBox2.ImageLocation = textBox1.Text;
string filepath = textBox1.Text;
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
image = br.ReadBytes((int)fs.Length);
string sql = " INSERT INTO ImageTable(Image) VALUES(@Imgg)";
if (con.State != ConnectionState.Open)
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add(new SqlParameter("@Imgg", image));
int x= cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(x.ToString() + "Image saved");
}
}
I am not getting error in code. the image got converted and entry is done in database but says < Binary Data > at the sql database.
我没有收到代码错误。图像被转换并在数据库中完成输入,但在 sql 数据库中显示 <二进制数据>。
采纳答案by Jeena
The selected file stream should be converted to byte array.
所选文件流应转换为字节数组。
FileStream FS = new FileStream(filepath, FileMode.Open, FileAccess.Read); //create a file stream object associate to user selected file
byte[] img = new byte[FS.Length]; //create a byte array with size of user select file stream length
FS.Read(img, 0, Convert.ToInt32(FS.Length));//read user selected file stream in to byte array
Now this works fine. Database still shows < Binary data > but it could be converted back to image using memorystream method.
现在这工作正常。数据库仍然显示<二进制数据>,但可以使用内存流方法将其转换回图像。
Thanks to all...
谢谢大家...
回答by majimenezp
Try with something like this:
尝试这样的事情:
byte[] fileBytes=System.IO.File.ReadAllBytes("path to file");
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("insert into table(blob,filename) values (@blob,@name)");
command.Parameters.AddWithValue("blob", fileBytes);
command.Parameters.AddWithValue("name", "filename");
command.ExecuteNonQuery();
回答by GarethD
Assuming that you are using VARBINARY
to store your image (which you should be), you may need to make your SqlParameter more strongly typed:
假设您正在使用VARBINARY
来存储您的图像(您应该这样做),您可能需要使您的 SqlParameter 具有更强的类型:
So instead of
所以代替
cmd.Parameters.Add(new SqlParameter("@Imgg", image));
You would use:
你会使用:
cmd.Parameters.Add("@Imgg", SqlDbType.VarBinary).Value = (SqlBinary)image;
You could also use System.IO.File.ReadAllBytesto shorten your code turning a file path into a byte array.
您还可以使用System.IO.File.ReadAllBytes来缩短将文件路径转换为字节数组的代码。
回答by Jeena
Execute this stored procedure:
执行这个存储过程:
create procedure prcInsert
(
@txtEmpNo varchar(6),
@photo image
)
as
begin
insert into Emp values(@txtEmpNo, @photo)
end
Table Emp:
表 Emp:
create table Emp
(
[txtEmpNo] [varchar](6) NOT NULL,
imPhoto image
)