通过C#将图像添加到Blob(MySQL) [英] Adding image to blob (mysql) via c#
问题描述
我已经制作了一个C#应用程序,可以将硬盘中的图像添加到mysql中的blob字段中.我没有收到错误,并且斑点字段已填充.
I have made a C# application that adds images from the harddisk to a blob-field in mysql. I don't get errors and the blob-field is filled.
当我在MySQL Workbench中检查blob字段时,它说该字段不正确(无法显示图像).
When I check the blob-field in MySQL Workbench it says the field is not correct (it can not show the image).
无论我做什么,无论我尝试什么示例,似乎都无济于事.因此,没有必要在此处放置任何示例代码,因为到目前为止没有任何工作.
Whatever I do, whatever example I try, nothing seems to work. So it has no point to place any example code here, because nothing is working so far.
有人有我可以尝试的良好示例代码吗?
Does anyone have a good example code that I can try?
谢谢
推荐答案
好吧,由于某些人希望显示一些代码,因此我在Alex链接中描述的帮助下创建了一个非常简单的控制台应用程序.我从字面上获取了该代码,并将其放置在控制台应用程序中. 奇迹发生了,因为它一启动我的应用程序就起作用了.我不知道我在主应用程序中做错了什么,但这必须是某事(duh).这是代码:
Okay, since some people wanted some code to be shown, I created a very simple console application with the help described in the link of Alex. I literally taken that code and placed in the console app. Some miracle happend, because it worked as soon as I started the app. I don't know what I did wrong in the main application, but it has to be something (duh). Here is the code:
string MyConString = "SERVER=localhost;" +
"DATABASE=database;" +
"UID=root;" +
"PASSWORD=pass;";
System.IO.FileStream fs = new FileStream(@"D:\link\to\image.png", FileMode.Open);
System.IO.BufferedStream bf = new BufferedStream(fs);
byte[] buffer = new byte[bf.Length];
bf.Read(buffer, 0, buffer.Length);
byte[] buffer_new = buffer;
MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
MySqlCommand command = new MySqlCommand("", connection);
command.CommandText = "insert into table(fldImage) values(@image);";
command.Parameters.AddWithValue("@image", buffer_new);
command.ExecuteNonQuery();
connection.Close();
Console.WriteLine("Task Performed!");
Console.ReadLine();
这篇关于通过C#将图像添加到Blob(MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!