通过C#应用程序将图像保存在MySQL中 [英] Save Image in MySQL via C# application
问题描述
我在C#中有一个客户端应用程序,该应用程序用于获取图像(DataType: VarChar)
的位置.然后,应该假定该应用程序调用Web服务,该服务随后会将图像(而不是位置)存储在本地MySQL数据库中.
I have a client application in C#, which is meant to take in the location of an image (DataType: VarChar)
. This application is then supposed to invoke a web service, which in turn will store the Image (NOT the location) in a local MySQL Database.
问题是,我意识到我能够将所有其他数据从表单传递到数据库(除了图像外)……有人可以指出我在这里做错了吗?任何帮助将不胜感激.
The problem is, I realized that i am able to pass all the other data from the form to the database EXCEPT for the image...Can anyone please point out what I am doing wrong here? Any help will be greatly appreciated.
ps-我知道你们中很多人会建议我将图像保存在文件系统中,而不是保存在数据库本身上....但是事实是,我的数据库无论如何都不大,所以保存图像希望数据库本身不会有什么大不了的:)
ps - I know that a lot of you will be suggesting me to save the images in a file system rather than on the database itself....but the thing is, my database is not that big anyways so saving the images on the database itself will not be that big of a deal hopefully :)
这是我在MySQL中的表,
Here is my table in MySQL,
create table testImage(
id int not null auto_increment,
name varchar(50),
age int,
image blob,
primary key(id));
这是WebMethod
,用于将数据插入表中...当image
字段被注释掉时,它可以工作.
And here is the WebMethod
which is meant to insert the data into the table...it works when the image
field is commented out.
[WebMethod]
public string sendDataToMySql(string get_name, int get_age, byte[] buffer)
{
string MyConString = "SERVER=localhost;" +
"DATABASE=test;" +
"UID=root;" +
"PASSWORD=password;";
string name_new = get_name;
int age_new = get_age;
byte[] buffer_new = buffer;
MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
MySqlCommand command = new MySqlCommand("", connection);
command.CommandText = "insert into testdata(name, age, image) values(@name, @age, @image);";
command.Parameters.AddWithValue("@name", name_new);
command.Parameters.AddWithValue("@age", age_new);
command.Parameters.AddWithValue("@image", buffer_new);
command.ExecuteNonQuery();
connection.Close();
return "Task Performed!";
}
推荐答案
我认为您根本不需要声明buffer_new
变量,您可以直接使用buffer
参数.
I don't think you need to declare the buffer_new
variable at all, you can simply use the buffer
parameter as it is.
我的猜测是,您应该将MySql.Data.MySqlClient.MySqlDbType.Blob
数据类型分配给@Image
参数,而不仅仅是AddWithValue
...
my guess is that you should assign the MySql.Data.MySqlClient.MySqlDbType.Blob
data type to the @Image
parameter not just AddWithValue
...
在此处查看完整示例:将Blob插入MySQL
check here for a full example: Insert blob into MySQL
这篇关于通过C#应用程序将图像保存在MySQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!