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
查看全文
相关阅读:
python全栈学习--day31(正则)
python 全栈开发,Day26(hashlib文件一致性,configparser,logging,collections模块)
python全栈学习--面向对象进阶3
python全栈学习--面向对象进阶2
面向对象进阶
python全栈学习--Day19(面向对象组合,继承)
python全栈学习--Day18(面向对象交互)
python全栈学习--Day17(初识面向对象)
python全栈学习--day15(递归函数,二分查找法)
python-函数
原文地址:https://www.cnblogs.com/flashicp/p/746136.html
最新文章
VBox添加虚拟磁盘挂载
基于oslo_log的日志管理
SpringBoot点滴(1)
带参装饰器、可迭代对象、迭代器对象、for 迭代器工作原理、枚举对象、生成器及生成表达式
函数的嵌套定义,global,nonlocal关键字的使用,闭包及闭包的运算场景,装饰器
三元表达式, 函数对象,名称空间与作用域,函数的嵌套定义
函数的参数列表
函数的介绍与简单的使用
文件处理,内存管理机制,
python的基本数据类型
热门文章
深浅拷贝,元组类型,字典类型,字符编码
深浅拷贝,元祖,字典 ,集合的定义以及基本操作方法
字符串基本操作,数据类型介绍与基本使用
day06
day05
day04
day03
day02
day01
python全栈学习--Day30 (纸牌游戏,异常和错误,异常处理)
Copyright © 2011-2022 走看看