下载Oracle数据库中的Blob二进制文件,实例!

类别:.NET开发 点击:0 评论:0 推荐:

将Oracle数据库中HR方案下的TESTFILEUPLOAD表中的文件下载至系统临时目录。

void downloadFile()
??{
???OracleConnection objOcon = new OracleConnection("user id=system;Data Source=tsems;password=system");

???OracleCommand objOcmd = new OracleCommand();

???objOcmd.Connection = objOcon;

???objOcmd.CommandText = "SELECT * FROM HR.TESTFILEUPLOAD WHERE PKID='2006'";

???FileStream objFs;

???BinaryWriter objBw;

???int bufferSize = 260;

???byte [] outByte = new byte[bufferSize];

???long retval;

???long startIndex = 0;

???string fileName = "";

???objOcon.Open();

???OracleDataReader objDr = objOcmd.ExecuteReader(CommandBehavior.SequentialAccess);
???objDr.Read();

?? fileName = objDr["FILENAME"].ToString();

???objFs = new FileStream(Path.GetTempPath()+fileName, FileMode.OpenOrCreate,FileAccess.Write);

???objBw = new BinaryWriter(objFs);

???startIndex = 0;

???retval = objDr.GetBytes(8, startIndex, outByte, 0, bufferSize);

???while ( retval == bufferSize)
???{
????objBw.Write(outByte);

????objBw.Flush();

????startIndex += bufferSize;

????retval = objDr.GetBytes(8, startIndex, outByte, 0, bufferSize);
???}

???objBw.Write(outByte, 0, (int)retval -1);

???objBw.Flush();

???objBw.Close();

???objFs.Close();

?? bjDr.Close();

???objOcon.Close();
??}

本文地址:http://com.8s8s.com/it/it44009.htm