将BLOB数据读写到Microsoft SQL或Oracle数据库






4.77/5 (15投票s)
在本文中,我将探讨如何将二进制文件(如图像或 PDF)存储和检索到 Microsoft SQL 或 Oracle 数据库中。
引言
在本文中,我将探讨如何将二进制文件(如图像或 PDF)存储和检索到 Microsoft SQL 或 Oracle 数据库中。
Using the Code
将文件读取到字节数组
byte[] byteArray = null;
using (FileStream fs = new FileStream
(FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byteArray = new byte[fs.Length];
int iBytesRead = fs.Read(byteArray, 0, (int)fs.Length);
}
将 BLOB 数据从文件保存到 Oracle
对于 Oracle,您需要从 Oracle 下载 ODP.NET。以下脚本将创建一个表,用于在 Oracle 中存储 Blob 数据。
CREATE TABLE BlobStore
(
ID number,
BLOBFILE BLOB,
DESCRIPTION varchar2(100)
);
现在,我们想使用 C# 将 Blob 写入 Oracle。
string sql = " INSERT INTO BlobStore(ID,BLOBFILE,DESCRIPTION) _
VALUES(:ID, :BLOBFILE, :DESCRIPTION) ";
string strconn = ConfigurationManager.ConnectionStrings_
["ConnectionString"].ConnectionString;
using (OracleConnection conn = new OracleConnection(strconn))
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
cmd.Parameters.Add("ID", OracleDbType.Int32, 1, ParameterDirection.Input);
cmd.Parameters.Add("BLOBFILE", OracleDbType.Blob,
byteArray , ParameterDirection.Input);
cmd.Parameters.Add("DESCRIPTION", OracleDbType.Varchar2,
"any thing here", ParameterDirection.Input);
cmd.ExecuteNonQuery();
}
}
在下一步中,我们想将数据从 Oracle 加载到文件。
string sql = " select * from BlobStore ";
string strconn = ConfigurationManager.ConnectionStrings_
["ConnectionString"].ConnectionString;
using (OracleConnection conn = new OracleConnection(strconn))
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
using (IDataReader dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
byte[] byteArray= (Byte[])dataReader["BLOBFILE"];
using (FileStream fs = new FileStream
(strfn, FileMode.CreateNew, FileAccess.Write))
{
fs.Write(byteArray, 0, byteArray.Length);
}
}
}
}
}
将 BLOB 数据从文件保存到 Microsoft SQL
在 SQL Server 中存储和检索 Blob 数据与 Oracle 类似。以下代码片段展示了在 SQL Server 中保存和加载的方法。以下脚本将创建一个表,用于在 SQL Server 中存储 Blob 数据。
CREATE TABLE TestTable
(
ID int,
BlobData varbinary(max),
DESCRIPTION nvarchar(100)
)
以下代码展示了如何从 SQL Server 加载到文件。
using (SqlConnection connection = new SqlConnection("ConnectionString"))
{
connection.Open();
using (SqlCommand command =
new SqlCommand("select BlobData from TestTable", connection))
{
byte[] buffer = (byte[])command.ExecuteScalar();
using (FileStream fs = new FileStream
(@"C:\test.pdf", FileMode.Create))
{
fs.Write(buffer, 0, buffer.Length);
}
}
}
以下代码展示了如何从字节数组保存到 SQL Server。
using (SqlConnection connection = new SqlConnection("ConnectionString"))
{
connection.Open();
using(SqlCommand cmd = new SqlCommand("INSERT INTO TestTable_
(ID, BlobData, DESCRIPTION) VALUES (@ID, @BlobData, @DESCRIPTION)", conn))
{
cmd.Parameters.Add("@ID", SqlDbType.int).Value = 1;
cmd.Parameters.Add("@BlobData", SqlDbType.VarBinary).Value = ByteArray;
cmd.Parameters.Add("@DESCRIPTION", SqlDbType.NVarchar).Value = _
"Any text Description";
cmd.ExecuteNonQuery();
}
}
摘要
在本文中,我们探讨了如何将二进制文件(如图像或 PDF)存储和检索到 Oracle 或 Microsoft SQL 数据库中。
历史
- 2009 年 12 月 22 日:初始发布