zoukankan
html css js c++ java
BLOB
using
System;
using
System.Data;
using
System.Text;
using
System.Windows.Forms;
using
System.Configuration;
using
System.Data.SqlClient;
using
System.IO;
namespace
LobTest
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
//
读取BLOB数据并写入文件模式
{
const
int
employeeIdColumn
=
0
;
const
int
employeePhotoColumn
=
1
;
//
bufferSize must be bigger than oleOffset
const
int
bufferSize
=
100
;
byte
[] buffer
=
new
byte
[bufferSize];
int
byteCountRead;
long
currentIndex
=
0
;
ConnectionStringSettings nwSetting
=
ConfigurationManager.ConnectionStrings[
"
NwString
"
];
using
(SqlConnection cn
=
new
SqlConnection())
{
cn.ConnectionString
=
nwSetting.ConnectionString;
cn.Open();
using
(SqlCommand cmd
=
cn.CreateCommand())
{
cmd.CommandText
=
"
SELECT EmployeeID, Photo FROM Employees
"
;
SqlDataReader rdr
=
cmd.ExecuteReader(
CommandBehavior.SequentialAccess);
while
(rdr.Read())
{
int
employeeId
=
rdr.GetInt32(employeeIdColumn);
string
fileName
=
@"
c:\Employee
"
+
employeeId.ToString().PadLeft(
2
,
'
0
'
)
+
"
.bin
"
;
//
Create a file to hold the output.
using
(FileStream fs
=
new
FileStream(
fileName, FileMode.OpenOrCreate,
FileAccess.Write))
{
currentIndex
=
0
;
byteCountRead
=
(
int
)rdr.GetBytes(employeePhotoColumn,
currentIndex, buffer,
0
, bufferSize);
while
(byteCountRead
!=
0
)
{
fs.Write(buffer,
0
, byteCountRead);
currentIndex
+=
byteCountRead;
byteCountRead
=
(
int
)rdr.GetBytes(employeePhotoColumn,
currentIndex, buffer,
0
, bufferSize);
}
}
}
}
}
MessageBox.Show(
"
Done
"
);
}
private
void
button2_Click(
object
sender, EventArgs e)
//
读取BLOB数据并写入文件模式删除OLE头信息
{
const
int
oleOffset
=
78
;
const
int
oleTypeStart
=
20
;
const
int
oleTypeLength
=
12
;
const
int
employeeIdColumn
=
0
;
const
int
employeePhotoColumn
=
1
;
const
int
bufferSize
=
100
;
//
must be bigger than oleOffset
byte
[] buffer
=
new
byte
[bufferSize];
int
bufferStart
=
0
;
int
byteCountRead;
long
currentIndex
=
0
;
ConnectionStringSettings nwSetting
=
ConfigurationManager.ConnectionStrings[
"
NwString
"
];
using
(SqlConnection cn
=
new
SqlConnection())
{
cn.ConnectionString
=
nwSetting.ConnectionString;
cn.Open();
using
(SqlCommand cmd
=
cn.CreateCommand())
{
cmd.CommandText
=
"
SELECT EmployeeID, Photo FROM Employees
"
;
SqlDataReader rdr
=
cmd.ExecuteReader(
CommandBehavior.SequentialAccess);
while
(rdr.Read())
{
int
employeeId
=
rdr.GetInt32(employeeIdColumn);
string
fileName
=
@"
c:\Employee
"
+
employeeId.ToString().PadLeft(
2
,
'
0
'
)
+
"
.bmp
"
;
//
Create a file to hold the output.
using
(FileStream fs
=
new
FileStream(
fileName, FileMode.OpenOrCreate,
FileAccess.Write))
{
currentIndex
=
0
;
//
read until we have the oleheader, if possible
while
(currentIndex
<
oleOffset)
{
byteCountRead
=
(
int
)rdr.GetBytes(employeePhotoColumn,
currentIndex, buffer, (
int
)currentIndex,
bufferSize
-
(
int
)currentIndex);
if
(byteCountRead
==
0
)
break
;
currentIndex
+=
byteCountRead;
}
byteCountRead
=
(
int
)currentIndex;
//
process oleheader, if it exists
if
(byteCountRead
>=
oleOffset)
{
string
type
=
Encoding.ASCII.GetString(
buffer, oleTypeStart, oleTypeLength);
if
(type
==
"
Bitmap Image
"
)
{
bufferStart
=
oleOffset;
byteCountRead
=
byteCountRead
-
oleOffset;
}
}
while
(byteCountRead
!=
0
)
{
fs.Write(buffer, bufferStart, byteCountRead);
bufferStart
=
0
;
byteCountRead
=
(
int
)rdr.GetBytes(employeePhotoColumn,
currentIndex, buffer,
0
, bufferSize);
currentIndex
+=
byteCountRead;
}
}
}
}
}
MessageBox.Show(
"
Done
"
);
}
private
void
button3_Click(
object
sender, EventArgs e)
//
写如BLOB数据
{
const
int
bufferSize
=
100
;
byte
[] buffer
=
new
byte
[bufferSize];
long
currentIndex
=
0
;
byte
[] photoPtr;
ConnectionStringSettings nwSetting
=
ConfigurationManager.ConnectionStrings[
"
NwString
"
];
using
(SqlConnection cn
=
new
SqlConnection())
{
cn.ConnectionString
=
nwSetting.ConnectionString;
cn.Open();
using
(SqlCommand cmd
=
cn.CreateCommand())
{
cmd.CommandText
=
"
SELECT TEXTPTR(Photo) FROM Employees WHERE EmployeeID = 1
"
;
photoPtr
=
(
byte
[])cmd.ExecuteScalar();
}
using
(SqlCommand cmd
=
cn.CreateCommand())
{
cmd.CommandText
=
"
UPDATETEXT Employees.Photo @Pointer @Offset null @Data
"
;
SqlParameter ptrParm
=
cmd.Parameters.Add(
"
@Pointer
"
, SqlDbType.Binary,
16
);
ptrParm.Value
=
photoPtr;
SqlParameter photoParm
=
cmd.Parameters.Add(
"
@Data
"
, SqlDbType.Image);
SqlParameter offsetParm
=
cmd.Parameters.Add(
"
@Offset
"
, SqlDbType.Int);
offsetParm.Value
=
0
;
using
(FileStream fs
=
new
FileStream(
"
Girl.gif
"
,
FileMode.Open, FileAccess.Read))
{
int
count
=
fs.Read(buffer,
0
, bufferSize);
while
(count
!=
0
)
{
photoParm.Value
=
buffer;
photoParm.Size
=
count;
cmd.ExecuteNonQuery();
currentIndex
+=
count;
offsetParm.Value
=
currentIndex;
count
=
fs.Read(buffer,
0
, bufferSize);
}
}
}
}
MessageBox.Show(
"
Done
"
);
}
}
}
查看全文
相关阅读:
HDU 2643 Rank:第二类Stirling数
HDU 4372 Count the Buildings:第一类Stirling数
HDU 3625 Examining the Rooms:第一类Stirling数
HDU 3682 To Be an Dream Architect:查重【三维坐标系中点在实数上的映射】
POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】
bzoj 1050 旅行comf
luogu 3958 奶酪
luogu 3952 时间复杂度
luogu 3951 小凯的疑惑
bzoj 1016 最小生成树计数
原文地址:https://www.cnblogs.com/zwl12549/p/869198.html
最新文章
Android-ListView-ArrayAdapter
CURL
lnmp更改网站文件和MySQL数据库的存放目录
MySQL连接缓慢,打开缓慢原因
PHP函数 ------ ctype_alnum
ECMA-262 Extractions
Node.js 文件输入
Django notes I: collection of links to the Django document
Bash Command 1: find
Dinic 算法钩沉
热门文章
LaTeX:毕设模版设计问题1 各级标题编号设为黑体
Code Jam 2017 Round 1A Problem B. Ratatouille
几个可以免费下载或阅读电子书的网站
hihoCoder #1047 Random Tree
hihoCoder #1117 战争年代
LightOJ 1132 Summing up Powers:矩阵快速幂 + 二项式定理
LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导
HDU 1005 Number Sequence:矩阵快速幂
POJ 2409 Let it Bead:置换群 Polya定理
HDU 1028 Ignatius and the Princess III:dp or 母函数
Copyright © 2011-2022 走看看