CSDN博客

img lmbosos

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

发表于2004/9/22 13:21:00  1134人阅读

分类: C#开发经验

将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();
  }

0 0

相关博文

我的热门文章

img
取 消
img