2011-05-30 37 views
4

我想在一個MySQL數據庫中插入一個PDF文件,在blobMySQL中的BLOB與DataSet在C#

這裏是我使用插入代碼(我用一個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); 
} 

當我想和創建原始文件,這是在我的斑點,似乎好好工作。但是當我想打開我的新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

+0

我覺得這是我的WebService的問題,因爲任何文件我想在MySQL中插入時,MySQL表示: [BLOB - 130] – prorace 2011-05-31 00:04:51

回答

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