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 - 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 :)
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));
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!";
}
I don't think you need to declare the buffer_new
variable at all, you can simply use the buffer
parameter as it is.
my guess is that you should assign the MySql.Data.MySqlClient.MySqlDbType.Blob
data type to the @Image
parameter not just AddWithValue
...
check here for a full example: Insert blob into MySQL