我在 C# 中有一个客户端应用程序,用于获取图像的位置(DataType: VarChar)
。然后,该应用程序应该调用一个 Web 服务,该服务反过来将图像(而不是位置)存储在本地 MySQL 数据库中。
问题是,我意识到我能够将所有其他数据从表单传递到数据库,除了图像......谁能指出我在这里做错了什么?任何帮助将不胜感激。
ps - 我知道你们中的很多人会建议我将图像保存在文件系统中,而不是数据库本身....但问题是,我的数据库并没有那么大,所以将图像保存在数据库中希望它本身不会有什么大不了的:)
这是我在 MySQL 中的表,
create table testImage(
id int not null auto_increment,
name varchar(50),
age int,
image blob,
primary key(id));
这WebMethod
是用于将数据插入表中的...当image
字段被注释掉时它可以工作。
[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!";
}