5

我正在使用以下代码插入 blob 字段:


MySql.Data.MySqlClient.MySqlConnection conn;
MySql.Data.MySqlClient.MySqlCommand cmd;

conn = new MySql.Data.MySqlClient.MySqlConnection();
cmd = new MySql.Data.MySqlClient.MySqlCommand();

string SQL;
int FileSize;
byte[] rawData;
FileStream fs;

conn.ConnectionString = "server=192.168.1.104;uid=root;" +
        "pwd=root;database=cady234;";

fs = new FileStream(@"d:\Untitled.gif", FileMode.Open, FileAccess.Read);
FileSize = (int)fs.Length;

rawData = new byte[FileSize];
fs.Read(rawData, 0, FileSize);
fs.Close();

conn.Open();

string strFileName = "test name";
SQL = "INSERT INTO file (file_name, file_size, file) VALUES ('" + strFileName + "', "+FileSize+", '"+rawData+"')";

cmd.Connection = conn;
cmd.CommandText = SQL;

cmd.ExecuteNonQuery();
conn.Close();

插入没问题,但使用“在查看器中打开值”时图像未显示:

在此处输入图像描述

4

2 回答 2

17

当您使用字符串连接时,二进制数据没有正确传递给您的插入 - 您会得到rawData.ToString()它可能只是打印出 TypeName (因此您的二进制数据长度为 13 个字节,而文件大小大于 3000 个字节);试试这个:

byte[] rawData = File.ReadAllBytes(@"d:\Untitled.gif");
FileInfo info = new FileInfo(@"d:\Untitled.gif");

int fileSize = Convert.ToInt32(info.Length);

using(MySqlConnection connection = new MySqlConnection("server=192.168.1.104;uid=root;pwd=root;database=cady234;"))
{
    using(MySqlCommand command = new MySqlCommand())
    {
        command.Connection = connection;
        command.CommandText = "INSERT INTO file (file_name, file_size, file) VALUES (?fileName, ?fileSize, ?rawData);";
        MySqlParameter fileNameParameter = new MySqlParameter("?fileName", MySqlDbType.VarChar, 256);
        MySqlParameter fileSizeParameter = new MySqlParameter("?fileSize", MySqlDbType.Int32, 11);
        MySqlParameter fileContentParameter = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);

        fileNameParameter.Value = "test name";
        fileSizeParameter.Value = fileSize;
        fileContentParameter.Value = rawData;

        command.Parameters.Add(fileNameParameter);
        command.Parameters.Add(fileSizeParameter);
        command.Parameters.Add(fileContentParameter);

        connection.Open();

        command.ExecuteNonQuery();

    }
}

我在这里为你介绍了几个概念;首先,如果您要一次加载所有二进制数据,只需使用静态方法File.ReadAllBytes - 它的代码要少得多。

其次,不需要每次都使用完全限定的命名空间——使用using指令

第三,(有点令人困惑)C# 中还有一条using 语句。这可确保实现IDisposable的任何对象在其自身之后被正确清理。在连接的情况下,如果您的命令成功或失败,它将显式调用 Close 和 Dispose。

最后,我已经参数化了您的查询。参数有用的原因有很多;它们有助于防止SQL 注入,并且在这种情况下,它们还应该确保您的数据类型得到正确处理。您可以阅读有关SqlParameter的更多信息(例如,MySqlParameter 是特定于数据库的实现,但使用相同的原理)。

经测试可与 MySQL 5.5.15、在 .Net 4 下运行的 MySQL Connector 5.2.7 一起使用

于 2012-11-03T11:11:28.940 回答
-4

这个怎么样:

这对我来说可以。
Exepte 我发现这对我来说是错误的。

<?php
// csv invoeren naar pos.

$CSV = "uitvoer.csv";
 // The CSV file has only 2 colums; The "Reference" and the image name (in my case the barcode with "thumb_" in front of it.

$username = "Username";
$password = "Passwwoorrdd";
$database = "POS";
$counter = 0;

// Create connection
$conn = new mysqli("localhost", $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully <br>";

//$mysqli->select_db();
ini_set('max_execution_time', 1000); //300 seconds = 5 minutes

if (($handle = fopen($CSV, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1050, ",")) !== FALSE) {
        // this loops through each line of your csv, putting the values into array elements

    $counter++;

$IDEE = $data[0]; 
        // It seems that after opening the image the $data is mixed up.

$imag = "photos/".$data[1];  // where "photos/' the folder is where this php file gets executed (mostly in /var/www/ of /var/www/html/)

$fh = fopen($imag, "r");
$data = addslashes(fread($fh, filesize($imag)));
fclose($fh);

echo " Ref: ".$IDEE." ----".$counter."----<br>";
   // If there will be a time-out. You could erase the part what is already done minus 1.
$sql =  "UPDATE PRODUCTS SET IMAGE='".$data."' WHERE CODE=$IDEE";

// 带有 IMAGE 的表 PRODUCTS。该表中有更多数据。但我只需要更新图像。其余的已经插入。

if ($conn->query($sql) === TRUE) {
    echo "Tabel <b>products</b> updated successfully<br>";
} else {
    echo "<br>Error updating tabel <b>Products</b>: " . $conn->error;
Exit();
}
    }
    fclose($handle);
}
?>
于 2016-04-27T13:48:07.353 回答