我用的是oracle,该字段类型为BLOB型,整个htm文件全都存在该字段里,
现在我要把它读出来并显示出来,请问该怎样实现,请写详细点。谢谢!
我的代码如下:
OleDbConnection myConnection = ………………; //连接数据库
OleDbDataReader reader=null;
string sql;
sql = "SELECT SYSTEMTITLE,SYSTEMCONTENT FROM T_TrainSafeSystem WHERE ID = '" + Request.QueryString["ID"] + "'";
OleDbCommand cmd = new OleDbCommand(sql,myConnection);
FileStream fs; // Writes the BLOB to a file (*.htm).
BinaryWriter bw; // Streams the BLOB to the FileStream object.
int bufferSize = 200000; // Size of the BLOB buffer.
byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes.
long retval; // The bytes returned from GetBytes.
long startIndex = 0; // The starting position in the BLOB output.
string pub_id = ""; // The publisher id to use in the file name.
// Open the connection and read data into the DataReader.
reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while (reader.Read())
{
// Get the publisher id, which must occur before getting the logo.
pub_id = reader.GetString(0);
// Create a file to hold the output.
fs = new FileStream("制度:" + pub_id + ".htm", FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
// Reset the starting byte for the new BLOB.
startIndex = 0;
// Read the bytes into outbyte[] and retain the number of bytes returned.
retval = reader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
// Continue reading and writing while there are bytes beyond the size of the buffer.
while (retval == bufferSize)
{
bw.Write(outbyte);
bw.Flush();
// Reposition the start index to the end of the last buffer and fill the buffer.
startIndex+= bufferSize;
retval = reader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
}
// Write the remaining buffer.
bw.Write(outbyte);
bw.Flush();
// Close the output file.
bw.Close();
fs.Close();
}
// Close the reader and the connection.
reader.Close();
执行到 reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);时出现错误:
“不支持此接口发生了一个 Oracle 错误,但无法从 Oracle 中检索错误信息。数据类型不被支持。”
请问这是怎么回事?是不是在 相应的FORM里要设置什么属性啊?我在FORM标签里写了
enctype="multipart/form-data" 。我不知道错在哪??请各位指点!!!
2 个解决方案
#1
79、如果 要读取 oracle 数据库里的 BLOB 类型的字段内容,就不能使用 .net 自带的 System.Data.OleDb 类库的类,要使用 OracleClient (它是一个专门用于操作管理Oracle数据库的类库,类似于SqlClient类库)的类(OracleConnection, OracleCommand,OracleDataAdapter,.net 不自带 OracleClient ,需要添加 System.Data.OracleClient.dll 引用才能使用,这个文件可到 MSDN 上下载。
我这有一段参考代码:
OracleConnection myConnection = new OracleConnection("Password=Test;User ID=Test;Data Source= myDataBase");
string sql;
sql = "SELECT SYSTEMTITLE,SYSTEMCONTENT FROM T_TrainSafeSystem WHERE ID = '" + Request.QueryString["ID"] + "'";
OracleCommand cmd = new OracleCommand(sql,myConnection);
OracleDataAdapter adapter = new OracleDataAdapter(sql,myConnection);
DataSet ds = new DataSet();
adapter.Fill(ds,"showText");
byte[] sysRuler = (byte[])ds.Tables["showText"].Rows[0]["SYSTEMCONTENT"];
Response.BinaryWrite(sysRuler);
反正我这段代码把图片从数据库里读出并显示出来没问题,但读出htm文件在显示时有一些乱码,你看看有无借鉴,如果你把乱码问题解决了请告诉我,呵呵!
我这有一段参考代码:
OracleConnection myConnection = new OracleConnection("Password=Test;User ID=Test;Data Source= myDataBase");
string sql;
sql = "SELECT SYSTEMTITLE,SYSTEMCONTENT FROM T_TrainSafeSystem WHERE ID = '" + Request.QueryString["ID"] + "'";
OracleCommand cmd = new OracleCommand(sql,myConnection);
OracleDataAdapter adapter = new OracleDataAdapter(sql,myConnection);
DataSet ds = new DataSet();
adapter.Fill(ds,"showText");
byte[] sysRuler = (byte[])ds.Tables["showText"].Rows[0]["SYSTEMCONTENT"];
Response.BinaryWrite(sysRuler);
反正我这段代码把图片从数据库里读出并显示出来没问题,但读出htm文件在显示时有一些乱码,你看看有无借鉴,如果你把乱码问题解决了请告诉我,呵呵!
#2
参考:
cmd.CommandText = "SELECT * FROM tablewithlobs";
OracleDataReader reader = cmd.ExecuteReader();
using(reader)
{
reader.Read();
OracleLob BLOB = reader.GetOracleLob(1);
OracleLob CLOB = reader.GetOracleLob(2);
OracleLob NCLOB = reader.GetOracleLob(3);
byte[] buffer = new byte[100];
while((actual = blob.Read(buffer, 0, buffer.Length)) >0)
Console.WriteLine(blob.LobType + ".Read(" + buffer + ", " + buffer.Length + ") => " + actual);
StreamReader streamreader = new StreamReader(clob, Encoding.Unicode);
char[] cbuffer = new char[100];
while((actual = streamreader.Read(cbuffer, 0, cbuffer.Length)) >0)
Console.WriteLine(CLOB.LobType + ".Read(" + new string(cbuffer, 0, actual) + ", " + cbuffer.Length + ") => " + actual);
Console.WriteLine(NCLOB.LobType + ".Value => " + NCLOB.Value);
}
cmd.CommandText = "SELECT * FROM tablewithlobs";
OracleDataReader reader = cmd.ExecuteReader();
using(reader)
{
reader.Read();
OracleLob BLOB = reader.GetOracleLob(1);
OracleLob CLOB = reader.GetOracleLob(2);
OracleLob NCLOB = reader.GetOracleLob(3);
byte[] buffer = new byte[100];
while((actual = blob.Read(buffer, 0, buffer.Length)) >0)
Console.WriteLine(blob.LobType + ".Read(" + buffer + ", " + buffer.Length + ") => " + actual);
StreamReader streamreader = new StreamReader(clob, Encoding.Unicode);
char[] cbuffer = new char[100];
while((actual = streamreader.Read(cbuffer, 0, cbuffer.Length)) >0)
Console.WriteLine(CLOB.LobType + ".Read(" + new string(cbuffer, 0, actual) + ", " + cbuffer.Length + ") => " + actual);
Console.WriteLine(NCLOB.LobType + ".Value => " + NCLOB.Value);
}
#1
79、如果 要读取 oracle 数据库里的 BLOB 类型的字段内容,就不能使用 .net 自带的 System.Data.OleDb 类库的类,要使用 OracleClient (它是一个专门用于操作管理Oracle数据库的类库,类似于SqlClient类库)的类(OracleConnection, OracleCommand,OracleDataAdapter,.net 不自带 OracleClient ,需要添加 System.Data.OracleClient.dll 引用才能使用,这个文件可到 MSDN 上下载。
我这有一段参考代码:
OracleConnection myConnection = new OracleConnection("Password=Test;User ID=Test;Data Source= myDataBase");
string sql;
sql = "SELECT SYSTEMTITLE,SYSTEMCONTENT FROM T_TrainSafeSystem WHERE ID = '" + Request.QueryString["ID"] + "'";
OracleCommand cmd = new OracleCommand(sql,myConnection);
OracleDataAdapter adapter = new OracleDataAdapter(sql,myConnection);
DataSet ds = new DataSet();
adapter.Fill(ds,"showText");
byte[] sysRuler = (byte[])ds.Tables["showText"].Rows[0]["SYSTEMCONTENT"];
Response.BinaryWrite(sysRuler);
反正我这段代码把图片从数据库里读出并显示出来没问题,但读出htm文件在显示时有一些乱码,你看看有无借鉴,如果你把乱码问题解决了请告诉我,呵呵!
我这有一段参考代码:
OracleConnection myConnection = new OracleConnection("Password=Test;User ID=Test;Data Source= myDataBase");
string sql;
sql = "SELECT SYSTEMTITLE,SYSTEMCONTENT FROM T_TrainSafeSystem WHERE ID = '" + Request.QueryString["ID"] + "'";
OracleCommand cmd = new OracleCommand(sql,myConnection);
OracleDataAdapter adapter = new OracleDataAdapter(sql,myConnection);
DataSet ds = new DataSet();
adapter.Fill(ds,"showText");
byte[] sysRuler = (byte[])ds.Tables["showText"].Rows[0]["SYSTEMCONTENT"];
Response.BinaryWrite(sysRuler);
反正我这段代码把图片从数据库里读出并显示出来没问题,但读出htm文件在显示时有一些乱码,你看看有无借鉴,如果你把乱码问题解决了请告诉我,呵呵!
#2
参考:
cmd.CommandText = "SELECT * FROM tablewithlobs";
OracleDataReader reader = cmd.ExecuteReader();
using(reader)
{
reader.Read();
OracleLob BLOB = reader.GetOracleLob(1);
OracleLob CLOB = reader.GetOracleLob(2);
OracleLob NCLOB = reader.GetOracleLob(3);
byte[] buffer = new byte[100];
while((actual = blob.Read(buffer, 0, buffer.Length)) >0)
Console.WriteLine(blob.LobType + ".Read(" + buffer + ", " + buffer.Length + ") => " + actual);
StreamReader streamreader = new StreamReader(clob, Encoding.Unicode);
char[] cbuffer = new char[100];
while((actual = streamreader.Read(cbuffer, 0, cbuffer.Length)) >0)
Console.WriteLine(CLOB.LobType + ".Read(" + new string(cbuffer, 0, actual) + ", " + cbuffer.Length + ") => " + actual);
Console.WriteLine(NCLOB.LobType + ".Value => " + NCLOB.Value);
}
cmd.CommandText = "SELECT * FROM tablewithlobs";
OracleDataReader reader = cmd.ExecuteReader();
using(reader)
{
reader.Read();
OracleLob BLOB = reader.GetOracleLob(1);
OracleLob CLOB = reader.GetOracleLob(2);
OracleLob NCLOB = reader.GetOracleLob(3);
byte[] buffer = new byte[100];
while((actual = blob.Read(buffer, 0, buffer.Length)) >0)
Console.WriteLine(blob.LobType + ".Read(" + buffer + ", " + buffer.Length + ") => " + actual);
StreamReader streamreader = new StreamReader(clob, Encoding.Unicode);
char[] cbuffer = new char[100];
while((actual = streamreader.Read(cbuffer, 0, cbuffer.Length)) >0)
Console.WriteLine(CLOB.LobType + ".Read(" + new string(cbuffer, 0, actual) + ", " + cbuffer.Length + ") => " + actual);
Console.WriteLine(NCLOB.LobType + ".Value => " + NCLOB.Value);
}