zoukankan
html css js c++ java
将文件以二进制形式存储到Sql Server中
/DatSet读取文件/
#region
/DatSet读取文件/
/**/
///
<summary>
///
根据aaId,使用DataSet 读取**文件内容,还原恢复。
///
</summary>
///
<param name="ioId">
**的ID号
</param>
///
<param name="tableName">
表名
</param>
///
<param name="fieldName">
存储**文件内容的字段
</param>
public
bool
ReadFileOutFormSqlServer(
int
ioId,
string
tableName,
string
fieldName)
{
bool
flag
=
false
;
object
tempfileName
=
null
;
string
strSql
=
"
select fullpath,
"
+
fieldName
+
"
from
"
+
tableName
+
"
where infoID=
"
+
infoId;
DataSet ds
=
new
DataSet();
Database database
=
new
Database(p_sqlConnString);
try
{
ds
=
database.DBDataSet(strSql);
byte
[] tmpFile
=
null
;
foreach
(DataRow dr
in
ds.Tables[
0
].Rows)
{
if
(dr[
0
]
!=
DBNull.Value)
{
tempfileName
=
dr[
0
];
tmpFile
=
(
byte
[])dr[
1
];
}
}
ds.Dispose();
if
(tmpFile
!=
null
)
{
string
fileName
=
tempfileName.ToString();
FileInfo fi
=
new
System.IO.FileInfo(fileName);
if
(
!
fi.Exists)
{
//
新建文件
using
(FileStream fstream
=
fi.Create())
{
int
length
=
tmpFile.Length;
fstream.Write(tmpFile,
0
,tmpFile.Length);
fstream.Close();
flag
=
true
;
}
}
else
{
using
(FileStream fs
=
fi.OpenWrite())
{
int
length
=
tmpFile.Length;
fs.Write(tmpFile,
0
,tmpFile.Length);
fs.Close();
flag
=
true
;
}
}
}
}
catch
(Exception err)
{
throw
err;
}
finally
{
database.Close();
}
return
flag;
}
#endregion
/***保存文件***/
#region
/***保存文件***/
/**/
///
<summary>
///
保存文件到SQL Server数据库中
///
</summary>
///
<param name="RRR">
**文件类
</param>
///
<param name="tableName">
表名
</param>
///
<param name="fieldName">
存储**文件内容的字段
</param>
public
bool
SaveIntoSqlServer(RRR accredit,
string
tableName,
string
fieldName)
{
bool
flag
=
false
;
//
保存文件到SQL Server数据库中
string
fileName
=
accredit.FullPath;
FileInfo fi
=
new
FileInfo(fileName);
if
(fi.Exists)
{
SqlConnection cn
=
null
;
try
{
byte
[] dData
=
null
;
//
读文件,放入buffer
using
(FileStream fs
=
fi.OpenRead())
{
dData
=
new
byte
[fi.Length];
int
nReadLength
=
fs.Read(dData,
0
,(
int
)(fi.Length));
}
string
sqlStr
=
"
insert into
"
+
tableName
+
"
(ioID,path,cTime,
"
+
fieldName
+
"
) values(
"
+
accredit.IoID
+
"
,'
"
+
accredit.Path
+
"
','
"
+
accredit.CTime
+
"
',@file)
"
;
cn
=
ConnectToSqlSever();
if
(cn.State
==
0
) cn.Open();
SqlCommand cm
=
new
SqlCommand(sqlStr,cn);
cm.Parameters.Add(
"
@file
"
,dData);
cm.ExecuteNonQuery();
cm.Dispose();
flag
=
true
;
}
catch
(Exception err)
{
throw
err;
}
finally
{
cn.Close();
}
}
return
flag;
}
#endregion
数据字段为varbinary
查看全文
相关阅读:
构建之法阅读笔记02
学习进度条
构建之法阅读笔记01
c++ 与C的区别
c++ 菜单动态效果
c++ 方框中绘制菜单代码
c++ 绘制方框
c++ 条件编译
c++ 预处理和多重替换
c++ 文件共享打开
原文地址:https://www.cnblogs.com/flashicp/p/746136.html
最新文章
GIL全局解释器锁+GIL全局解释器锁vs互斥锁+定时器+线程queue+进程池与线程池(同步与异步)
网络编程
Python 基础知识----面向对象编程
TYVJ1071 LCIS 线性DP+决策集优化
LG3004 「USACO2010DEC」Treasure Chest 区间DP+滚动数组优化
LG2770/LOJ6122 航空路线问题 费用流 网络流24题
LG2512/BZOJ1045 「HAOI2008」糖果传递 中位数
LG2053/BZOJ1070 「SCOI2007」修车 费用流
LG5338/BZOJ5509/LOJ3105 「TJOI2019」甲苯先生的滚榜 Treap
LG4516/LOJ2546 「JSOI2018」潜入行动 树上背包
热门文章
LG2045 方格取数加强版 费用流
LG1640 「SCOI2010」连续攻击游戏 二分图最大匹配
LG2602/BZOJ1833 「ZJOI2010」数字计数 数位DP
返回一个整数数组中最大子数组的和
构建之法阅读笔记04
Right-BICEP要求设计四则运算2
构建之法阅读笔记03
软件工程个人作业03
课堂作业(求几个数的最大值)
软件工程个人作业02
Copyright © 2011-2022 走看看