2

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

        }
4

3 回答 3

4

我认为你根本不需要声明buffer_new变量,你可以简单地使用buffer参数。

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

在此处查看完整示例:将 blob 插入 MySQL

于 2011-12-13T20:14:48.190 回答
1

Blob 是一个十六进制值

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

一种方法是在插入查询中将字节数组传递给十六进制字符串,而不使用 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();
}
于 2013-03-13T15:55:48.867 回答
0
System.IO.BufferedStream bf = new BufferedStream(httpFile.InputStream);
byte[] buffer = new byte[bf.Length];   
bf.Read(buffer,0,buffer.Length);    

然后将缓冲区传递给您的输入语句。数据类型需要是 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;
    }           
}
于 2011-12-13T20:22:09.143 回答