MySQL 中的 BLOB 与 C# 中的数据集

发布于 2024-11-10 16:48:00 字数 1410 浏览 0 评论 0原文

我想在 MySQL 数据库中的 blob 中插入 PDF 文件。

下面是我用来插入的代码(我使用 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 中获取信息的代码。我收到数据集 (_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

I would like to insert a PDF file in a MySQL database, in a blob.

Here is the code I'm using to insert (I use a WebService and a 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);
}

When I want to get and create the original file with which is in my blob, it seems to work well. But when I want to open my new PDF file, Adobe indicates that the file is not supported or is corrupted ...

Here is the code I'm using to get information from my blob. I receive informations in a 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();

How do I fix this problem? I think the problem is where I insert, because I can see 13o in my blob.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话已封尘 2024-11-17 16:48:00

我必须使用参数化请求:

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();

I had to use a parameterized request :

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文