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
查看全文
相关阅读:
路径总和 III(力扣第437题)
HBase的存储文件合并(StoreFile Compaction)、Region Split
二叉树的层平均值、 找树左下角的值(力扣第637题、513题)
HBase的读写数据流程
HBase的内存数据刷写MemStore Flush
翻转二叉树、合并二叉树(力扣第226、617题)
最长同值路径(力扣第687题)
CSS字体动态炫彩样式代码
Redis基础
MySQL数据库基本操作【3】
原文地址:https://www.cnblogs.com/flashicp/p/746136.html
最新文章
granfna展示mysql数据源
关于运维工程师岗位的定义和思考(转载)
SSH安全信任
SpringBoot2.0+Shiro+MyBatisPlus权限管理系统
马哥博客作业第十二周
Java连载135-JDBC执行SQL操作
搭建一个开源项目11-Kubernetes集群部署(中)
搭建一个开源项目10-Kubernetes集群部署(上)
C连载19-Printf中的标记举例
Android连载29-持久化技术
热门文章
MATLAB学习记录2
mac几个状态产看指令
django报错: ‘set’ object is not reversible
csrf攻击与防御
session 、cookie、token的区别
csrf攻击原理
mac-python-tesseract-亦可用于验证码识别
WC后NOI前的一些训练记录和想法
WC2020游记
0801-0803训练记录
Copyright © 2011-2022 走看看