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
查看全文
相关阅读:
重构与反思-<重构代码的7个阶段>有感
Unity 自定义"=="操作符 [翻译来源blogs.unity3d,2014/05]
Unity UGUI Button 无法点击问题一例
[Lua性能] 小试验一例
C# 循环中 直接索引 VS 缓存索引 性能测试
Lua table直接索引VS缓存索引性能测试小示例
大型网站架构系列:负载均衡详解(1)
大型网站架构系列:电商网站架构案例(3)
大型网站架构系列:电商网站架构案例(2)
大型分布式网站架构技术总结
原文地址:https://www.cnblogs.com/flashicp/p/746136.html
最新文章
生成项目目录结构
mysql8 :客户端连接caching-sha2-password问题
Liquibase的简单使用
Nginx反向代理实现Tomcat负载均衡
gitlab启用https
腾讯云Centos安装gitlab
腾讯云Centos安装nginx
腾讯云Centos安装jdk8
SpringBoot整合Mybatis
SpringBoot入门,新建SpringBoot项目
热门文章
Beyond Compare 4【破解】
WebStorm换主题(护眼)
Vue的安装并在WebStorm中运行
Spring对注解(Annotation)处理【转】
Redis实例
设计模式
定义/控制流程的代码绝无可能复用-除非是有继承/替代关系的类
书单
[待补充]面向接口编程,数据驱动编程
关于代码整理重构小记
Copyright © 2011-2022 走看看