2011-06-07 56 views

回答

1

有兩點要注意:

1)您應該使用參數化查詢

//Assuming you already have a connection somewhere that is opened. 

var sql = "INSERT INTO mytable (name, theblob) VALUES (?, ?);"; 

using (var command = new IfxCommand(sql, connection)) 
{ 
    command.Parameters.Add(new IfxParameter()).Value = "foo"; 
    command.Parameters.Add(new IfxParameter()).Value = ifxBlob; 
} 


幾件事情需要注意:Informix的有一個錯誤,當它涉及到客戶端SDK 3.5.xC7 (至今)。在插入過程中,您可以輕鬆地傳入字節數組,但如果傳入byte []數組,則在執行更新時將會發生609錯誤。相反,您必須使用 IfxBlob對象。

public IfxBlob CreateIfxBlob(byte[] data) 
{ 
    //Get the connection however you like and make sure it's open... 
    //Obviously you should make this method handle exceptions and the such. 

    IfxBlob blob = connection.GetIfxBlob(); 

    blob.Open(IfxSmartLOBOpenMode.ReadWrite); 
    blob.Write(data); 
    blob.Close(); 

    return blob; 
} 

您必須在更新過程中傳入IfxBlob,因此在插入過程中也可以這樣做。

此外,請記住,如果您嘗試設置null,您將收到錯誤消息。相反,如果值爲null,則傳入DBNull.Value。

public Object AsDBValue(Object value) //Or this Object value if you want it as an extension 
{ 
    if (value == null) 
     return DBNull.Value; 

    //Other checks 
    if (value is Enum) 
     return Convert.ChangeType(value, value.GetType().GetEnumUnderlyingType()); 

    //Is Blob? 
    if (value is byte[]) 
     return GetIfxBlob(value as byte[]); 

    return value; 
} 

不指定參數的類型

//These will lead to errors unless you typecast the parameters in the query. 
new IfxParameter { IfxType = IfxType.Blob }; 
new IfxParameter { DbType = DbType.Binary }; 

如果你做任何的那些,你就必須做到以下幾點:

  • 當Blob值不爲空"INSERT INTO mytable (name, theblob) VALUES (?, ?::blob);";
  • 當Blob值爲空"INSERT INTO mytable (name, theblob) VALUES (?, ?::byte);";

你可以看到,這將是一個痛苦的對接有因爲類型和價值的不同的查詢。只要不指定DbType或IfxType並讓Informix .NET提供程序爲您映射正確的類型(即使它不能在Update上正確映射byte []數組)。

希望能爲你解決問題,因爲我經歷了同樣的痛苦,試圖找出並發現我認爲是Informix .NET提供程序(Ver:3.5.xC7)中的錯誤。

+0

將其更改爲getblob函數並且它工作正常! – michael 2011-06-20 18:39:22

1

最好的辦法是使用參數化查詢。例如:

using(System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("INSERT INTO mytable (name, theblob) VALUES ('foo', @binaryValue)", conn)) 
{ 
    cmd.Parameters.Add("@binaryValue", System.Data.SqlDbType.Text, 8000).Value = arraytoinsert; 
    cmd.ExecuteNonQuery(); 
} 

我已經假設您的列類型爲Text。 上述方法的原始信用是從this post

+0

這似乎不起作用。它以@binaryValue的形式提供它,而不是替換參數。 – michael 2011-06-11 13:55:12

+0

嘗試將添加的第二個參數更改爲'System.Data.SqlDbType.VarBinary'。你可以發佈你的sql表定義和你正在嘗試使用的代碼嗎? – IndigoDelta 2011-06-13 08:30:30

0

我讀了你兩個消息,這是解決方案,在幫助我:

byte[] data = File.ReadAllBytes(PathFile); 

StringBuilder sql = new StringBuilder(); 

sql.Append(" UPDATE updater SET report = ? where path = " + "\'" + Path + "\' and status = 1;"); 
IfxCommand cmd = new IfxCommand(sql.ToString(), i_connect); 
cmd.Parameters.Add("@binaryValue", IfxType.Byte).Value = data; 
int res = cmd.ExecuteNonQuery(); 

PathFile - 是FILE.TXT

我的Informix表:

CREATE TABLE updater 
(
    nzp_up SERIAL PRIMARY KEY, 
    version VARCHAR(50), 
    status INT, 
    path VARCHAR(200), 
    key VARCHAR(100), 
    soup VARCHAR(20), 
    report TEXT 
); 
-1

這個職位是真正有用的解決我的問題,所以我想分享我的解決方案,它可能會幫助其他人。以下是完整的代碼:

 try 
     { 
      //pFoto is a byte[] loaded in another method. 
      if (pFoto != null && pFoto.Length > 0) 
      { 
       StringBuilder sentenciaSQL = new StringBuilder(); 
       sentenciaSQL.Append("INSERT INTO bd_imagenes:imagenes "); 
       sentenciaSQL.Append("(identificador, cod_imagen, fecha_desde, fecha_hasta, fecha_grabacion, usuario, sec_transaccion, imagen) "); 
       sentenciaSQL.Append("VALUES (?, 'FP', current, null, current, ?, 0, ?);"); 

       using (IfxConnection conIFX = new IfxConnection("Database=bd_imagenes; Server=xxxxxxxx; uid=xxxxxxx; password=xxxxxxxx; Enlist=true; Client_Locale=en_US.CP1252;Db_Locale=en_US.819")) 
       { 
        conIFX.Open(); //<- Abro la conexion. 
        //Aqui convierto la foto en un BLOB:       
        IfxBlob blob = conIFX.GetIfxBlob(); 
        blob.Open(IfxSmartLOBOpenMode.ReadWrite); 
        blob.Write(pFoto); 
        blob.Close(); 

        //Creo el Comando con la SQL: 
        using (IfxCommand cmd = new IfxCommand(sentenciaSQL.ToString(), conIFX)) 
        { 
         //Agrego los parámetros en el mismo orden que la SQL: 
         cmd.Parameters.Add(new IfxParameter()).Value = pCedula; 
         cmd.Parameters.Add(new IfxParameter()).Value = SecurityHandler.Auditoria.NombreUsuario; 
         cmd.Parameters.Add(new IfxParameter()).Value = blob; 
         //Ejecuto la Consulta: 
         Resultado = cmd.ExecuteNonQuery(); 
        } 
        conIFX.Close(); 
       } 
       if (Resultado != 0) { retorno = true; } 
      } 
     } 
     catch (IfxException ae) 
     { 
      if (exepcionesValidacion == null) { exepcionesValidacion = new ArrayList(); } 
      exepcionesValidacion.Add(Util.CrearExcepcion(ae.Message, "ERROR_INESPERADO", ae.StackTrace)); 
     } 
     catch (Exception ex) 
     { 
      if (exepcionesValidacion == null) { exepcionesValidacion = new ArrayList(); } 
      exepcionesValidacion.Add(Util.CrearExcepcion(ex.Message, "ERROR_INESPERADO", ex.StackTrace)); 
     } 
     return retorno; 
    } 
相關問題