从 Windows Form C# 中的 oracle 获取 BLOB 文件
Getting BLOB file from oracle in Windows Form C#
我是 C# 的菜鸟,并尝试创建项目以 win 形式从 oracle 下载 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();
}
这有什么问题吗?
如果我尝试将一个简单的代码写入 select 文件并将其显示在标签上,该代码工作正常。
提前致谢
我找到了:问题是我在 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# 的菜鸟,并尝试创建项目以 win 形式从 oracle 下载 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();
}
这有什么问题吗? 如果我尝试将一个简单的代码写入 select 文件并将其显示在标签上,该代码工作正常。 提前致谢
我找到了:问题是我在 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();
}