asp.net - 在 Windows 窗体 C# 中从 oracle 获取 BLOB 文件
问题描述
我是 C# 中的菜鸟,并尝试创建项目以从 oracle 以 win 形式下载 blob 文件并将文件保存在本地下载文件夹中。
这是我的代码
string tempDir = "\\\\NB17-KP-239\\Downloads\\";
for (int index = 0; index < dataTable.Rows.Count; ++index)
{
string oradbConString = "Data Source = localhost; Persist Security Info = True; User ID = homeuser; Password = admin;";
OracleConnection oraCon = new OracleConnection(oradbConString);
oraCon.Open();
string path;
using (oraCon)
{
using (OracleCommand comOra = oraCon.CreateCommand())
{
comOra.CommandText = "select id,name,contenttype from blob_sample where id = 3";
//comOra.Parameters.Add("Id", dataTable.Rows[index]["id"]);
OracleDataReader oracleDataReader = comOra.ExecuteReader();
oracleDataReader.Read();
OracleBlob oracleBlob = oracleDataReader.GetOracleBlob(1);
using (TempFileCollection tempFileCollection = new TempFileCollection(tempDir, false))
{
path = tempFileCollection.AddExtension("file", true);
FileStream fileStream = new FileStream(path, FileMode.Create);
byte[] buffer = new byte[oracleBlob.Length];
int count = oracleBlob.Read(buffer, 0, Convert.ToInt32(oracleBlob.Length));
fileStream.Write(buffer, 0, count);
fileStream.Close();
}
}
oraCon.Close();
}
那有什么问题?如果我尝试一个简单的代码来选择文件并将其显示在标签上,则代码工作正常。提前致谢
解决方案
我找到了:问题是我在GetOracleBlob
数组中选择了元素 (1),而正确的元素是 (0)。
string tempDir = string tempDir = @"D:\Test";
for (int index = 0; index < dataTable.Rows.Count; ++index)
{
//this.AppendText("download start : " + reqBy + " < " + appBy + "\r\n");
string oradbConString = "Data Source = localhost; Persist Security Info = True; User ID = homeuser; Password = admin;";
OracleConnection oraCon = new OracleConnection(oradbConString);
oraCon.Open();
string path;
using (oraCon)
{
using (OracleCommand comOra = oraCon.CreateCommand())
{
comOra.CommandText = "select data from blob_sample where id = 4";
OracleDataReader oracleDataReader = comOra.ExecuteReader();
oracleDataReader.Read();
OracleBlob oracleBlob = oracleDataReader.GetOracleBlob(0);
using (TempFileCollection tempFileCollection = new TempFileCollection(tempDir, false))
{
path = tempFileCollection.AddExtension("file", true);
FileStream fileStream = new FileStream(path, FileMode.Create);
byte[] buffer = new byte[oracleBlob.Length];
int count = oracleBlob.Read(buffer, 0, Convert.ToInt32(oracleBlob.Length));
fileStream.Write(buffer, 0, count);
fileStream.Close();
}
}
oraCon.Close();
}
推荐阅读
- c# - C# 如何使用 datagridview_comboboxes 填充数据网格视图
- r - 有条件地从 R 中的列表中删除项目
- excel - InStr 在特定单元格中查找确切数字
- javascript - '-' 在 v-bind:style / Vue.js 中
- reactjs - 反应路由器
不添加活动类 - matplotlib - 在 matplot lib 上以灰度显示图像
- firebase - 如何在 map() 中使用异步代码(Flutter、Firestore)
- c++ - 如何在文件中将 Hi 替换为 Bye
- python - Python:用“常规”替代品替换最小化的特殊字符
- r - 在 R 中具有多个条件的 count_if (EXPSS)