当前位置: 代码迷 >> ASP.NET >> OracleDataReader 有关问题
  详细解决方案

OracleDataReader 有关问题

热度:4079   发布时间:2013-02-25 00:00:00.0
OracleDataReader 问题
使用OracleDataReader时,提示Invalid operation. The connection is closed.
搞不懂什么意思,代码如下:请帮助。

public void oledboracle_datareader()
{
long blobDataSize = 0; //BLOB数据体实际大小
long readStartByte = 0;//从BLOB数据体的何处开始读取数据
int bufferStartByte = 0;//将数据从buffer数组的何处开始写入
int hopeReadSize = 1024; //希望每次从BLOB数据体中读取数据的大小
long realReadSize = 0;//
string connectionstring="data source=web;user=web;password=123;";  
//写连接串
OracleConnection conn=new OracleConnection(connectionstring);
//创建一个新连接
OracleCommand cmd= new OracleCommand("select s_001 from x_w where t_001 = 00000001",conn);
OracleDataReader dr =cmd.ExecuteReader(CommandBehavior.SequentialAccess);


byte[] buffer = null;
while (dr.Read())
{
blobDataSize = dr.GetBytes(0, 0, null, 0, 0); //获取这个BLOB数据体的总大小
buffer = new byte[blobDataSize];
realReadSize = dr.GetBytes(0, readStartByte, buffer, bufferStartByte, hopeReadSize);
//循环,每次读取1024byte大小
while ((int)realReadSize == hopeReadSize)
{
bufferStartByte += hopeReadSize;
readStartByte += realReadSize;
realReadSize= dr.GetBytes(0, readStartByte, buffer, bufferStartByte, hopeReadSize);
}
//读取BLOB数据体最后剩余的小于1024byte大小的数据
dr.GetBytes(0, readStartByte, buffer, bufferStartByte, (int)realReadSize);
//读取完成后,BLOB数据体的二进制数据就转换到这个byte数组buffer上去了
}


conn.Close();

}

------解决方案--------------------------------------------------------
哪句话出问题了?
OracleDataReader dr =cmd.ExecuteReader(CommandBehavior.SequentialAccess); 这句???

------解决方案--------------------------------------------------------
LZ你有conn.Close(),conn.Open()呢?
------解决方案--------------------------------------------------------
public void oledboracle_datareader()
{
long blobDataSize = 0; //BLOB数据体实际大小 
long readStartByte = 0;//从BLOB数据体的何处开始读取数据 
int bufferStartByte = 0;//将数据从buffer数组的何处开始写入 
int hopeReadSize = 1024; //希望每次从BLOB数据体中读取数据的大小 
long realReadSize = 0;// 
string connectionstring = "data source=web;user=web;password=123;";
//写连接串 
OracleConnection conn = new OracleConnection(connectionstring);
//创建一个新连接 
OracleCommand cmd = new OracleCommand("select s_001 from x_w where t_001 = 00000001", conn);
conn.Open();
OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

byte[] buffer = null;
while (dr.Read())
{
blobDataSize = dr.GetBytes(0, 0, null, 0, 0); //获取这个BLOB数据体的总大小 
buffer = new byte[blobDataSize];
realReadSize = dr.GetBytes(0, readStartByte, buffer, bufferStartByte, hopeReadSize);
//循环,每次读取1024byte大小 
while ((int)realReadSize == hopeReadSize)
{
bufferStartByte += hopeReadSize;
readStartByte += realReadSize;
realReadSize = dr.GetBytes(0, readStartByte, buffer, bufferStartByte, hopeReadSize);
}
//读取BLOB数据体最后剩余的小于1024byte大小的数据 
dr.GetBytes(0, readStartByte, buffer, bufferStartByte, (int)realReadSize);
//读取完成后,BLOB数据体的二进制数据就转换到这个byte数组buffer上去了 
}
conn.Close();
}