当前位置:网站首页>C# ODBC将一个文件夹的文件装载到PostgreSQL数据库BLOB列,并将BLOB列下载到另一个文件夹
C# ODBC将一个文件夹的文件装载到PostgreSQL数据库BLOB列,并将BLOB列下载到另一个文件夹
2022-04-21 17:32:00 【allway2】

using System.Data.Odbc;
string SQL;
long FileSize;
byte[] rawData;
FileStream fs;
//Connection string for Connector/ODBC 3.51
string MyConString = "DSN=PostgreSQL35W;" +
"UID=postgres;" +
"PWD=postgres;";
//Connect to MySQL using Connector/ODBC
OdbcConnection conn = new OdbcConnection(MyConString);
OdbcCommand cmd = new OdbcCommand();
try
{
// Get the directory
DirectoryInfo place = new DirectoryInfo(@"InputFiles\");
// Using GetFiles() method to get list of all
// the files present in the Train directory
FileInfo[] Files = place.GetFiles();
Console.WriteLine("Files are:");
Console.WriteLine();
// Display the file names
foreach (FileInfo i in Files)
{
Console.WriteLine("File Name - {0}", i.Name);
{
fs = new FileStream(i.FullName, FileMode.Open, FileAccess.Read);
FileSize = fs.Length;
rawData = new byte[FileSize];
fs.Read(rawData, 0, (int)FileSize);
fs.Close();
conn.Open();
SQL = "set names 'utf8'";
cmd.Connection = conn;
cmd.CommandText = SQL;
cmd.ExecuteNonQuery();
SQL = "INSERT INTO bindata(name,data) VALUES( ?, ?)";
cmd.Connection = conn;
cmd.CommandText = SQL;
cmd.Parameters.Clear();
cmd.Parameters.Add("name", OdbcType.NVarChar).Value = i.Name;
cmd.Parameters.Add("data", OdbcType.VarBinary).Value = rawData;
cmd.ExecuteNonQuery();
Console.WriteLine("File Inserted into database successfully!",
"Success!");
conn.Close();
}
}
}
catch (OdbcException MyOdbcException) //Catch any ODBC exception ..
{
for (int i = 0; i < MyOdbcException.Errors.Count; i++)
{
Console.Write("ERROR #" + i + "\n" +
"Message: " +
MyOdbcException.Errors[i].Message + "\n" +
"Native: " +
MyOdbcException.Errors[i].NativeError.ToString() + "\n" +
"Source: " +
MyOdbcException.Errors[i].Source + "\n" +
"SQL: " +
MyOdbcException.Errors[i].SQLState + "\n");
}
}
using System.Data.Odbc;
string SQL;
long FileSize;
byte[] rawData;
FileStream fs;
//Connection string for Connector/ODBC 3.51
string MyConString = "DSN=PostgreSQL35W;" +
"UID=postgres;" +
"PWD=postgres;";
//Connect to MySQL using Connector/ODBC
OdbcConnection conn = new OdbcConnection(MyConString);
OdbcCommand cmd = new OdbcCommand();
OdbcDataReader myData;
SQL = "SELECT name, data FROM bindata";
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = SQL;
myData = cmd.ExecuteReader();
while (myData.Read())
{
FileSize = myData.GetBytes(myData.GetOrdinal("data"), 0, null, 0, 0);
rawData = new byte[FileSize];
myData.GetBytes(myData.GetOrdinal("data"), 0, rawData, 0, (int)FileSize);
fs = new FileStream(@"DownLoadFiles\" + myData.GetString(0), FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(rawData, 0, (int)FileSize);
fs.Close();
Console.WriteLine(myData.GetString(0), "File successfully written to disk!", "Success!");
}
myData.Close();
conn.Close();
}
catch (OdbcException MyOdbcException) //Catch any ODBC exception ..
{
for (int i = 0; i < MyOdbcException.Errors.Count; i++)
{
Console.Write("ERROR #" + i + "\n" +
"Message: " +
MyOdbcException.Errors[i].Message + "\n" +
"Native: " +
MyOdbcException.Errors[i].NativeError.ToString() + "\n" +
"Source: " +
MyOdbcException.Errors[i].Source + "\n" +
"SQL: " +
MyOdbcException.Errors[i].SQLState + "\n");
}
}
版权声明
本文为[allway2]所创,转载请带上原文链接,感谢
https://blog.csdn.net/allway2/article/details/124327006
边栏推荐
- 故障分析 | Federated 存储引擎表导致监控线程处于 Opening table 状态
- Excel表格中重要的数据如何隐藏不显示
- 幹貨 | 實戰演練基於加密接口測試測試用例設計
- Priority of keyword execution of MySQL query statement
- The R language uses the grepl function to check whether the substring exists in the specified string and the string matches. It is responsible for searching whether the given string object contains a
- Spark SQL底层执行流程详解
- [sogaf] sogaf architecture type / mode
- BUUCTF WEB [GXYCTF2019]BabyUpload
- 结构体malloc bug
- pytorch index_add_用法介绍
猜你喜欢

pytorch index_ add_ Usage introduction

pytorch index_add_用法介绍

诚邀报名丨首期OpenHarmony开发者成长计划分享日

为什么可以用概率分布密度函数来表示概率?
![[dynamic programming] 152 Product maximum subarray](/img/8f/8063f2bedaa89217ad0adf1988972d.png)
[dynamic programming] 152 Product maximum subarray

MySQL基础合集
本田北美产品规划发布!Insight混动停产,CR-V、雅阁即将发布

Excel表格中重要的数据如何隐藏不显示

Why can we use probability distribution density function to express probability?

Quick MTF, lens image quality test application
随机推荐
Control @ schedule on in different environments
. net core enterprise wechat web page authorization login
pytorch index_add_用法介绍
幹貨 | 實戰演練基於加密接口測試測試用例設計
Quick MTF, lens image quality test application
MSIL 静态类在 IL 定义上和非静态类的差别
【机器学习】门控循环单元
Detection, tracking, behavior recognition all in one! Industrial pedestrian analysis system is heavy and open source!
About the internal supposition
本田北美产品规划发布!Insight混动停产,CR-V、雅阁即将发布
Dry goods | actual combat drill based on encryption interface test case design
洞见科技首批通过央行国家金融科技测评中心「联邦学习」产品评测,实现「MPC+FL」金融应用双认证
MySQL sets 2 primary keys
第118章 SQL函数 REVERSE
Authentication bypass vulnerability in JIRA seraph (cve-2022-0540)
A field set by MySQL cannot be duplicate
关于内推想说的
DPI发布AI药物研发最新进展报告
@Transient
mysql设置2个主键