MySQL中的BLOB与C#中的DataSet

4
我希望您能够在MySQL数据库中插入一个PDF文件,存储为blob。以下是我用于插入的代码(我使用了WebService和DataSet):
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read);

byte[] MyData = new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));

fs.Close();

this._requete = "INSERT INTO stage_abstract(etuid, anac, pdf)
                 VALUES (" + 6 + ", " + 2009 + ", \"" + MyData + "\")";

int suc = 0;
using (this._ds)
{
    suc = this._dbo.InsertUpdateDelete(ws, this._requete);
}

当我想获取并创建我的 blob 中的原始文件时,似乎工作正常。但是当我想打开我的新 PDF 文件时,Adobe 指示文件不受支持或已损坏...
这是我用来从我的 blob 获取信息的代码。我在 DataSet(_ds)中接收信息:
this._requete = "SELECT stage_abstract.pdf as pdf
                 FROM stage_abstract
                 WHERE stage_abstract.etuid = " + 6 + "
                   AND stage_abstract.anac = " + 2009;

using (this._ds)
{
    this._ds = this._dbo.select(this.ws, this._requete);
}

byte[]  MyData = (byte[]) this._ds.Tables[0].Rows[0]["pdf"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);

FileStream fs = new FileStream(@"C:\essairecup.pdf"
                     , FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close();

我该如何解决这个问题?我认为问题出在我的插入位置,因为我可以在我的 blob 中看到 13o


我认为我的 WebService 存在问题,因为无论我想要插入什么文件到 MySql 中,MySql 都会显示: [BLOB - 13o] - prorace
1个回答

2

我必须使用参数化请求:

int tfeid = 222;
        string fileName = "myFile";
        string fullFileName = @"C:\myFile.pdf";

        FileStream fs = new FileStream(fullFileName, FileMode.OpenOrCreate, FileAccess.Read);
        byte[] MyData = new byte[fs.Length];
        fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
        fs.Close();

        DbProviderFactory MFactory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");
        DbConnection Mconnect = MFactory.CreateConnection();
        Mconnect.ConnectionString = ConfigurationManager.ConnectionStrings["ConnexionMySql"].ConnectionString;
        Mconnect.Open();
        DbCommand Mcommand = Mconnect.CreateCommand();
        Mcommand.CommandText = "UPDATE tfe_abstract SET pdf = ?paramImg, pdfnom = \"" + fileName + "\" WHERE tfeid = " + tfeid;

        DbParameter parametre = Mcommand.CreateParameter();
        parametre.ParameterName = "paramImg";
        parametre.Value = MyData;
        Mcommand.Parameters.Add(parametre);

        int result = Mcommand.ExecuteNonQuery();

        Mconnect.Close();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接