将二进制数据插入火鸟BLOB字段
本文关键字:BLOB 字段 火鸟 插入 二进制 数据 | 更新日期: 2023-09-27 17:49:38
我尝试将pdf文件记录到火鸟数据库中,但在火鸟中字段类型为BLOB和c#代码没有此选项。遵循我的代码:
public void databaseFilePut(string varFilePath){
OleDbConnection cn = new OleDbConnection("MinhaConexao");
byte[] imagem;
FileStream fs = new FileStream(varFilePath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(varFilePath).Length;
imagem = br.ReadBytes((int)numBytes);
string _sql = "insert into GD_ARQUIVODOC (GD_ARQDOCARQUIVO) values (?)";
cn.Open();
OleDbCommand cmd = new OleDbCommand(_sql, cn);
cmd.Parameters.Add("?", OleDbType.VarBinary).Value = imagem;
cmd.ExecuteNonQuery();
cn.Close();
这有什么问题?错误是:参数值不可读。为什么?有人能帮我吗?
在IBProvider站点上检查这个示例:
using (OleDbConnection con = ConnectionProvider.CreateConnection())
{
con.Open();
OleDbTransaction trans = con.BeginTransaction ();
OleDbCommand cmd = new OleDbCommand(...);
//....
OleDbParameter blob = new OleDbParameter();
blob.OleDbType = OleDbType.LongVarBinary;
blob.Value = /// a byte[]
cmd.Parameters.Add(blob);
cmd.ExecuteNonQuery();
trans.Commit();
}
请允许我在这个答案中使用我们自己的。net提供商:)
using System;
using System.Data;
using lcpi.data.oledb; //LCPI .NET Data Provider for OLEDB
namespace ConsoleApplication1
{
class Program
{
private const string c_cn_str
="provider=LCPI.IBProvider.3;"
+"location=localhost:d:''database''ibp_test_fb25_d3.gdb;"
+"user id=gamer;"
+"password=vermut;"
+"dbclient_library=fbclient.dll";
static void Main(string[] args)
{
try
{
using(var cn=new OleDbConnection(c_cn_str))
{
cn.Open();
using(var tr=cn.BeginTransaction(IsolationLevel.RepeatableRead))
{
using(var cmd=new OleDbCommand("",cn,tr))
{
cmd.CommandText
="insert into BIN_BLOB_TABLE (BIN_DATA) values(:bin)'n"
+"returning TEST_ID'n"
+"into :id";
cmd["bin"].Value=new byte[]{1,2,3};
cmd.ExecuteNonQuery();
var rec_id=cmd["id"].Value;
cmd.CommandText
="select BIN_DATA from BIN_BLOB_TABLE where TEST_ID=:x";
cmd["x"].Value=rec_id;
var data=(byte[])cmd.ExecuteScalar();
for(int i=0;i!=data.Length;++i)
Console.WriteLine("[{0}]: {1}",i,data[i]);
}//using cmd
tr.Commit();
}//using tr
}//using cn
}
catch(Exception e)
{
Console.WriteLine("ERROR: {0} - {1}",e.Source,e.Message);
}//catch
}//Main
}//Program
}//namespace ConsoleApplication1