首页 > 开发 > 综合 > 正文

将数据库中二进制数据以异步方式写入磁盘

2024-07-21 02:22:55
字体:
来源:转载
供稿:网友
方式一:一次获取,异步写入
/// <summary>
/// 缓冲区大小
/// </summary>
public const int numpixels = 512 * 512;
/// <summary>
/// 将数据文件写入磁盘
/// </summary>
/// <param name="strsql"></param>
/// <returns></returns>
public static bool makefilewithwritelistbyadapter(string strsql,out string strerr)
{
if(file.exists(configproxy.getvaluebykey("listfile")))file.delete(configproxy.getvaluebykey("listfile"));
datatable objtable;
if(!oledatabaseproxy.executesql(strsql,out objtable,out strerr))return false;
string outputpath = configproxy.getvaluebykey("outputpath");
if(objtable.rows.count < 1) return false;
string strdirectory = outputpath + "//";
if(!directory.exists(strdirectory)) directory.createdirectory(strdirectory);
for(int i = 0;i< objtable.rows.count; i ++)
{

string filename = objtable.rows[i]["附件名称"].tostring();
//记录输出列表
logproxy.writelist(strdirectory + filename);
//获取文件数据
byte [] imagecontent = (byte[])objtable.rows[i]["附件内容"];
autoresetevent manualevent = new autoresetevent(false);
filestream fstream =
new filestream(strdirectory + filename,filemode.create,
fileaccess.readwrite, fileshare.none, 4096, true);
iasyncresult asyncresult = fstream.beginwrite(
imagecontent, 0, imagecontent.length,
new asynccallback(endwritecallback),
new state(fstream, manualevent));
manualevent.waitone(5000, false);
fstream.close();
}
strerr = "";
return true;
}
class state
{
public filestream fstream;
public autoresetevent autoevent;

public state(filestream fstream, autoresetevent autoevent)
{
this.fstream = fstream;
this.autoevent = autoevent;
}
}
static void endwritecallback(iasyncresult asyncresult)
{

state stateinfo = (state)asyncresult.asyncstate;
int workerthreads;
int portthreads;
try
{
threadpool.getavailablethreads(out workerthreads,
out portthreads);
stateinfo.fstream.endwrite(asyncresult);
thread.sleep(1500);
}
finally
{
stateinfo.autoevent.set();
}
}

方式二:联机读取,异步写入

/// <summary>
/// 缓冲区大小
/// </summary>
public const int numpixels = 512 * 512;
/// <summary>
/// 将数据文件写入磁盘
/// </summary>
/// <param name="strsql"></param>
/// <returns></returns>
public static bool makefilewithwritelistbyreader(string strsql,out string strerr)
{
if(file.exists(configproxy.getvaluebykey("listfile")))file.delete(configproxy.getvaluebykey("listfile"));
string outputpath = configproxy.getvaluebykey("outputpath");
string strdirectory = outputpath + "//";
if(!directory.exists(strdirectory)) directory.createdirectory(strdirectory);
system.data.oledb.oledbcommand cmd = new oledbcommand();
oledbconnection cnn = new oledbconnection(configproxy.getvaluebykey("oleconnectionstring"));
cmd.connection = cnn;
cmd.commandtext = strsql;
//开启连接
try
{
cnn.open();
}
catch(exception err)
{
strerr = err.message;
return false;
}
byte[] pixels = new byte[numpixels];
oledbdatareader reader = cmd.executereader();
byte[]imagecontent;
//逐条处理
while(reader.read())
{
string filename = reader.getstring(1);
//记录输出列表
logproxy.writelist(strdirectory + filename);
//获取文件数据
imagecontent = new byte[convert.toint64(reader.getstring(7))];
reader.getbytes(6,0,imagecontent,0,convert.toint32(reader.getstring(7)));
autoresetevent manualevent = new autoresetevent(false);
filestream fstream =
new filestream(strdirectory + filename,filemode.create,
fileaccess.readwrite, fileshare.none, 4096, true);
iasyncresult asyncresult = fstream.beginwrite(
imagecontent, 0, imagecontent.length,
new asynccallback(endwritecallback),
new state(fstream, manualevent));
manualevent.waitone(5000, false);
fstream.close();
}
reader.close();
//关闭连接
if(cnn.state == system.data.connectionstate.open)
{
cnn.close();
}
strerr = "";
//释放资源
cnn.dispose();
cmd.dispose();
gc.collect();
return true;
}
class state
{
public filestream fstream;
public autoresetevent autoevent;

public state(filestream fstream, autoresetevent autoevent)
{
this.fstream = fstream;
this.autoevent = autoevent;
}
}
static void endwritecallback(iasyncresult asyncresult)
{

state stateinfo = (state)asyncresult.asyncstate;
int workerthreads;
int portthreads;
try
{
threadpool.getavailablethreads(out workerthreads,
out portthreads);
stateinfo.fstream.endwrite(asyncresult);
thread.sleep(1500);
}
finally
{
stateinfo.autoevent.set();
}
}

两种方式的比较:

方式一:适合于数据库负载较大,二进制数据大小已知的情况;
方式二:适合于数据库负载较小,二进制数据大小未知的情况;

其中:两种方式的异步机制都是相同的,没有任何区别;异步机制的优点在于充分发挥了操作系统的优点
注意:在需要对性能进行同比测试的上下文中不能采用异步机制而必须尽量采用同步机制,以提高真实性




发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表