Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过C#应用程序在MySQL中保存图像_C#_Mysql_Image Upload - Fatal编程技术网

通过C#应用程序在MySQL中保存图像

通过C#应用程序在MySQL中保存图像,c#,mysql,image-upload,C#,Mysql,Image Upload,我在C#中有一个客户机应用程序,用于获取图像的位置(数据类型:VarChar)。然后,该应用程序将调用一个web服务,该服务将把图像(而不是位置)存储在本地MySQL数据库中 问题是,我意识到我能够将表单中除图像之外的所有其他数据传递到数据库……有人能指出我在这里做错了什么吗?任何帮助都将不胜感激 ps-我知道很多人会建议我将图像保存在文件系统中,而不是数据库本身……但问题是,我的数据库没有那么大,所以将图像保存在数据库本身不会有什么大不了的,希望:) 这是我的MySQL表 create tab

我在C#中有一个客户机应用程序,用于获取图像的位置
(数据类型: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!";

        }

我认为您根本不需要声明
buffer\u new
变量,只需按原样使用
buffer
参数即可

我猜您应该将
MySql.Data.MySqlClient.MySqlDbType.Blob
数据类型分配给
@Image
参数,而不仅仅是
AddWithValue

请点击此处查看完整示例:

然后将缓冲区传递到输入语句中。数据类型需要是BLOB才能处理它。 您还需要获取mime类型,并对您想要支持的mime类型进行筛选,否则您可能会在数据库中遇到各种各样的问题

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;
    }           
}

Blob是一个十六进制值

一种方法是在insert查询中将字节数组传递给十六进制字符串,而不使用参数


blob存储您可能需要执行BinaryWriter让我再次检查一下您是如何生成字节数据的。。需要查看执行此操作的代码,以确保正确生成字节[]图像存储为BLOB…谢谢!链接是一个很大的帮助!但我有点困惑。在该链接上给出的最后一个代码片段…为什么需要这样做?另外,我如何引用
FileToArray
MySQL\u File\u Save
MimeType
…?链接现在似乎已失效。
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;
    }           
}
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();
}