我正在使用Informix和.NET开发工具包(C#):
基本上,在执行标准insert sql语句时,有什么方法可以插入blob吗?
INSERT INTO mytable (name, theblob) VALUES ('foo', ? what goes here ?);哦,我拥有的数据是byte[]数组形式的。
发布于 2011-06-07 23:16:40
最好的方法是使用参数化查询。例如:
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。
发布于 2011-11-21 18:27:24
我读了你的两条消息,这是对我有帮助的解决方案:
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
);发布于 2012-05-31 23:21:33
这篇文章对解决我的问题真的很有用,所以我想分享我的解决方案,它可能会对其他人有所帮助。下面是完整的代码:
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;
}https://stackoverflow.com/questions/6267159
复制相似问题