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
"
);
}
}
}
查看全文
相关阅读:
Java异常处理和设计
一次qps测试实践
Alternate Task UVA
Just Another Problem UVA
Lattice Point or Not UVA
Play with Floor and Ceil UVA
Exploring Pyramids UVALive
Cheerleaders UVA
Triangle Counting UVA
Square Numbers UVA
原文地址:https://www.cnblogs.com/zwl12549/p/869198.html
最新文章
File类_常见的方法(获取目录中指定规则的内容)_listFiles
File类_常见的方法(获取目录中指定规则的内容)
27-水池数目(dfs)
26-三个水杯(bfs)
7-set用法详解
25-最短步数(广搜)
12-阶乘的八进制位数(斯特林公式)
11-1,5,11,12的倍数(容斥原理)
10-取石子2
9-大数阶乘
热门文章
8-取石子1(围圈)
8-机器分配(hud4045-组合+第二类斯特林数)
pv qps 并发 的摘要
一次qps测试实践(续)
log4j重定向stdout和stderr到log文件
缓存在高并发场景下的常见问题
Java 8系列之重新认识HashMap (讲的最好)
HashMap的死循环
结合 ConcurrentHashMap.putIfAbsent 与 Futrue 实现本地缓存防击穿
ConcurrentMap.putIfAbsent(key,value) 用法讨论
Copyright © 2011-2022 走看看