首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL + Informix:如何在执行插入操作时添加blob?(使用.NET (C#) SDK)

SQL + Informix:如何在执行插入操作时添加blob?(使用.NET (C#) SDK)
EN

Stack Overflow用户
提问于 2011-06-07 22:55:09
回答 3查看 6.2K关注 0票数 2

我正在使用Informix和.NET开发工具包(C#):

基本上,在执行标准insert sql语句时,有什么方法可以插入blob吗?

代码语言:javascript
复制
INSERT INTO mytable (name, theblob) VALUES ('foo', ? what goes here ?);

哦,我拥有的数据是byte[]数组形式的。

EN

回答 3

Stack Overflow用户

发布于 2011-06-07 23:16:40

最好的方法是使用参数化查询。例如:

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2011-11-21 18:27:24

我读了你的两条消息,这是对我有帮助的解决方案:

代码语言:javascript
复制
 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表:

代码语言:javascript
复制
   CREATE TABLE updater
 (
    nzp_up SERIAL PRIMARY KEY,
    version VARCHAR(50),
    status INT,
    path VARCHAR(200),
    key VARCHAR(100),
    soup VARCHAR(20),
    report TEXT
 );
票数 0
EN

Stack Overflow用户

发布于 2012-05-31 23:21:33

这篇文章对解决我的问题真的很有用,所以我想分享我的解决方案,它可能会对其他人有所帮助。下面是完整的代码:

代码语言:javascript
复制
        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;
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6267159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档