我把select语句改成:select id from test where ID=2 OleDbDataReader myReader=myCommand.ExecuteReader();就不出错了,这说明是转换的问题。 我用byte[] bytes=(byte[])myCommand.ExecuteReader();编译不过,提示不能转换成byte[]
oledb不能读oracle的blob字段? 还有其他方法吗?Oracle Provider for .Net我没有用过,行吗?
Oracle.net组件里的例子 /////////////////////////////////////////////////////////////// // ReadLobExample // /////////////////////////////////////////////////////////////// public static void ReadLobExample(OracleCommand cmd) { int actual = 0; //Select some data. // Table Schema: // "CREATE TABLE tablewithlobs (a int, b BLOB, c CLOB, d NCLOB)"; // "INSERT INTO tablewithlobs values (1, 'AA', 'AAA', N'AAAA')"; cmd.CommandText = "SELECT * FROM tablewithlobs"; OracleDataReader reader = cmd.ExecuteReader(); using(reader) { //Obtain the first row of data. reader.Read();
//Obtain the LOBs (all 3 varieties). OracleLob BLOB = reader.GetOracleLob(1/*0:based ordinal*/); OracleLob CLOB = reader.GetOracleLob(2/*0:based ordinal*/); OracleLob NCLOB = reader.GetOracleLob(3/*0:based ordinal*/); //Example - Reading binary data (in chunks). byte[] buffer = new byte[100]; while((actual = blob.Read(buffer, 0/*buffer offset*/, buffer.Length/*count*/)) >0) Console.WriteLine(blob.LobType + ".Read(" + buffer + ", " + buffer.Length + ") => " + actual); //Example - Reading CLOB/NCLOB data (in chunks). //Note: You can read character data as raw Unicode bytes (using OracleLob.Read as in the above example). //However, because the OracleLob object inherits directly from the .Net stream object, //all the existing classes that manipluate streams can also be used. For example, the //.Net StreamReader makes converting the raw bytes into actual characters easier. StreamReader streamreader = new StreamReader(clob, Encoding.Unicode); char[] cbuffer = new char[100]; while((actual = streamreader.Read(cbuffer, 0/*buffer offset*/, cbuffer.Length/*count*/)) >0) Console.WriteLine(CLOB.LobType + ".Read(" + new string(cbuffer, 0, actual) + ", " + cbuffer.Length + ") => " + actual); //Example - Reading data (all at once). //You could use StreamReader.ReadToEnd to obtain all the string data,or simply //call OracleLob.Value to obtain a contiguous allocation of all the data. Console.WriteLine(NCLOB.LobType + ".Value => " + NCLOB.Value); } }
sr.Read(bytes, 0, bytes.Length);
sr.Close();//
myCommand.Parameters["@stream"].Value=bytes;
好像没有StreamReader.Length的方法吧?
向oracle添加成功了,但从oracle读出来的时候出错:
未指定的错误发生了一个 Oracle 错误,但无法从 Oracle 中检索错误信息。数据类型不被支持。
表是test(id number ,stream blob)
写入stream的代码:
FileStream fs = File.OpenRead("c:\\1.txt");
string strConnectDb;
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
System.Data.OleDb.OleDbConnection oleConnect = null;
strConnectDb = "Provider=MSDAORA;Password=manager;User ID=system;Data Source=zengting";
oleConnect=new OleDbConnection(strConnectDb);
oleConnect.Open();
string str="insert into test values(stream.nextval,?)";
OleDbCommand myCommand=new OleDbCommand(str,oleConnect);
myCommand.Parameters.Add(new OleDbParameter("@blob",OleDbType.Binary,bytes.Length,"blob"));
myCommand.Parameters["@blob"].Value=bytes;
myCommand.ExecuteNonQuery();
读出stream的代码:
string strConnectDb;
System.Data.OleDb.OleDbConnection oleConnect = null;
strConnectDb = "Provider=MSDAORA;Password=manager;User ID=system;Data Source=zen";
oleConnect=new OleDbConnection(strConnectDb);
oleConnect.Open();
string str="select stream from test where ID=2";
OleDbCommand myCommand=new OleDbCommand (str,oleConnect);
OleDbDataReader myReader=myCommand.ExecuteReader();//出错
请问,怎样读出来写到一个新的文件里?
如果读出正确的话,用下列语句保存byte[] bytes = (byte[])myReader["stream"];然后FileStream fs = new FileStream("c:\\456.txt",
FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close();
test(id number ,stream blob)
myCommand.Parameters.Add(new OleDbParameter("@blob",OleDbType.Binary,bytes.Length,"blob"));你的参数应该要写成
("@blob",OleDbType.Binary,bytes.Length,"stream"));
OleDbDataReader myReader=myCommand.ExecuteReader();
这句已经出错了。出错信息:未指定的错误发生了一个 Oracle 错误,但无法从 Oracle 中检索错误信息。数据类型不被支持。
OleDbDataReader myReader=myCommand.ExecuteReader();就不出错了,这说明是转换的问题。
我用byte[] bytes=(byte[])myCommand.ExecuteReader();编译不过,提示不能转换成byte[]
select stream from test;
STREAM
-----------
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
<BLOB>
32 rows selected
还有其他方法吗?Oracle Provider for .Net我没有用过,行吗?
// ReadLobExample
//
///////////////////////////////////////////////////////////////
public static void ReadLobExample(OracleCommand cmd)
{
int actual = 0; //Select some data.
// Table Schema:
// "CREATE TABLE tablewithlobs (a int, b BLOB, c CLOB, d NCLOB)";
// "INSERT INTO tablewithlobs values (1, 'AA', 'AAA', N'AAAA')";
cmd.CommandText = "SELECT * FROM tablewithlobs";
OracleDataReader reader = cmd.ExecuteReader();
using(reader)
{
//Obtain the first row of data.
reader.Read();
//Obtain the LOBs (all 3 varieties).
OracleLob BLOB = reader.GetOracleLob(1/*0:based ordinal*/);
OracleLob CLOB = reader.GetOracleLob(2/*0:based ordinal*/);
OracleLob NCLOB = reader.GetOracleLob(3/*0:based ordinal*/); //Example - Reading binary data (in chunks).
byte[] buffer = new byte[100];
while((actual = blob.Read(buffer, 0/*buffer offset*/, buffer.Length/*count*/)) >0)
Console.WriteLine(blob.LobType + ".Read(" + buffer + ", " + buffer.Length + ") => " + actual); //Example - Reading CLOB/NCLOB data (in chunks).
//Note: You can read character data as raw Unicode bytes (using OracleLob.Read as in the above example).
//However, because the OracleLob object inherits directly from the .Net stream object,
//all the existing classes that manipluate streams can also be used. For example, the
//.Net StreamReader makes converting the raw bytes into actual characters easier.
StreamReader streamreader = new StreamReader(clob, Encoding.Unicode);
char[] cbuffer = new char[100];
while((actual = streamreader.Read(cbuffer, 0/*buffer offset*/, cbuffer.Length/*count*/)) >0)
Console.WriteLine(CLOB.LobType + ".Read(" + new string(cbuffer, 0, actual) + ", " + cbuffer.Length + ") => " + actual); //Example - Reading data (all at once).
//You could use StreamReader.ReadToEnd to obtain all the string data,or simply
//call OracleLob.Value to obtain a contiguous allocation of all the data.
Console.WriteLine(NCLOB.LobType + ".Value => " + NCLOB.Value);
}
}
system.io.filestream f = new filestream(filepath,filmode.open)
if
{
bye[] bt = new byte[f.length];
int d = f.length;
f.read(bt,0,d);
oracleConnection conn = new oracleconnection("string");
conn.open()
oraclecommand commd = new oraclecommand;
commd.connection = conn;
sql = "insert tba value(:O_blob)";
system.data.oracleclient.oracleparameter op = new oracleparameter();
op.oracletype = oracletype.blob;
op.parametername = ":o_blob";
op.value = bt;
commd.parameters.add(op);
commd.exectenoquery();
conn.clsoe();
}
need:
system.data.oracleclient.dll
this in .net framework1.1