我尝试将pdf文件记录到firebird数据库中,但是在firebird中,字段是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();
这有什么不对吗?错误是:参数值不可读。为什么?有人可以帮帮我吗?暂时来说
答案 0 :(得分:0)
检查this example on the IBProvider site:
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();
}
答案 1 :(得分:0)
请允许我在这个答案中使用我们自己的.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