Save Image in MySQL via C# application

穿精又带淫゛_ 提交于 2019-12-11 04:00:43

问题


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!";

        }

回答1:


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




回答2:


Blob is a hexadecimal value

http://dev.mysql.com/doc/refman/5.0/en/hexadecimal-literals.html

One way is to pass the byte array to a hex string in insert query without using Parameters.Add ("@ Fileimage" MySqlDbType.MediumBlob);

MySqlConnection con = new MySqlConnection("Server=localhost;Database=bd_prueba;Uid=root;Pwd=Intel-IT;");
FileStream fs = new FileStream(@"D:\proyectos2.jpg", FileMode.Open, FileAccess.Read);

byte[] rawData = new byte[fs.Length];        
fs.Read(rawData, 0, (int)fs.Length);
fs.Close();
//byte[] to HEX STRING
string hex = BitConverter.ToString(rawData);
//'F3-F5-01-A3' to 'F3F501A3'
hex = hex.Replace("-", "");

if(con.State == con.Closed)
{
    con.Open();
}
//Standart VALUE HEX x'F3F501A3'
string SQL = @"INSERT INTO tabla(id,fileimage) VALUES ('stringhex',x'"+hex+"')";
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;            
cmd.CommandText = SQL;
cmd.ExecuteNonQuery();
if(con.State == con.Open)
{
    con.Close();
}



回答3:


System.IO.BufferedStream bf = new BufferedStream(httpFile.InputStream);
byte[] buffer = new byte[bf.Length];   
bf.Read(buffer,0,buffer.Length);    

then pass buffer in to your input statement. The datatype needs to be BLOB to handle it. You will need to get the mime type as well and have a filter of what mime types you want to support otherwise you could have all kinds of trouble with what goes in the database.

private string getFileExtension(string mimetype)
{
    mimetype = mimetype.Split('/')[1].ToLower();
    Hashtable hTable = new Hashtable();
    hTable.Add("pjpeg","jpg");
    hTable.Add("jpeg","jpg");
    hTable.Add("gif","gif");
    hTable.Add("x-png","png");
    hTable.Add("bmp","bmp");

    if(hTable.Contains(mimetype))
    {
        return (string)hTable[mimetype];
    }
    else
    {
        return null;
    }           
}


来源:https://stackoverflow.com/questions/8495482/save-image-in-mysql-via-c-sharp-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!