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
"
);
}
}
}
查看全文
相关阅读:
OnFileOpen与OnOpenDocument(转)
Wpf应用程序进入全屏和退出全屏
在WPF中使用Emgu加载Image<,>图像的两种方法
C#中ListBox控件重绘Item项
sdut2404 Super Prime ACM算法设计
真彩色制式下IplImage转成CBitmap格式
【转】中缀表达式转换为后缀表达式
MFC中CImage的简单复制方法 (Copy CImage)
齐鲁软件大赛尖峰时刻团队
Priest John's Busiest Day HDU2491 ACM算法设计
原文地址:https://www.cnblogs.com/zwl12549/p/869198.html
最新文章
走迷宫 SDUT1269 ACM算法设计
VS2010下MFC中配置GDI+
WPF 模拟键盘输入
机器人走迷宫 SDUT1590 ACM算法设计
Mayor's posters POJ2528 ACM算法设计
IplImage 与 CBitmap类 的相互转换
OGRE教程中的需要注意的地方(转)
关闭MFC对话框时删除自身
POJ1002 SDUT1001 4873279 ACM算法设计
HDU4415 Assassin’s Creed 2012ACM/ICPC 杭州赛区网络赛 F
热门文章
CImage 访问像素点 像素数据操作
小鼠迷宫问题 SDUT1157 ACM算法设计
(转)C#进行图像处理的几种方法(bitmap,bitmapData,IntPtr)
Ogre 小例子
将IplImage转换为DIB
OnFileOpen与OnOpenDocument
opencv2.2 MFC picture控件中显示图片
Pixel density sdut2411 ACM算法设计
HDU4268 2012ACM长春赛区网络赛 Alice and Bob
Kinect SDK 1.7新特性
Copyright © 2011-2022 走看看