📄 loadfile.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using AutoRefresh;
using System.Collections;
namespace AutoRefresh
{
/// <summary>
///
/// </summary>
public class LoadFile
{
private string sConnectString = "";//"Persist Security Info=False;User ID=sa;Initial Catalog=pubs;Data Source=60.63.208.231;Packet Size=4096";
private string sSqlCmdString = "SELECT file_name,file_info,file_size,file_ver,file_exe FROM dbo.TB_FileInfo1";
private int bufferSize = 50; //Size of the BLOB buffer.
private string sOutFileNameString = "";
private string sOutFilePathString = "";
private string sOutFileString = "";
private string sFileInfo = "";
private string sFileVer = "";
private long lTotalLoadByte = 0;
private long lFileSize = 0;
private long lrltSize = 0;
private long lindex = 0;
private long lpointer = 0;
private Form3 pform = null;
private string sOutMessage = "";
public LoadFile(Form3 tpform)
{
//
// TODO: 在此处添加构造函数逻辑
sConnectString = "Persist Security Info=False;User ID=sa;Initial Catalog=mysql;Data Source="+ Form1.static_Para.sServerName +";Packet Size=4096";
sOutFilePathString = Form1.static_Para.sFilePath;
pform = tpform;
Form1.static_Para.arrlst.Clear();
//
}
public string GetLastMessage()
{
return sOutMessage;
}
public bool Start()
{
try
{
SqlConnection sqlConn = new SqlConnection(sConnectString);
SqlCommand sqlCmd = new SqlCommand(sSqlCmdString,sqlConn);
FileStream fs;
BinaryWriter bw;
byte[] outbyte = new byte[bufferSize];
sqlConn.Open();
SqlDataReader sqlReader = sqlCmd.ExecuteReader(CommandBehavior.SequentialAccess);
while(sqlReader.Read())
{
lindex ++ ;
lpointer = 0;
lTotalLoadByte = 0;
sOutFileString = "";
lFileSize = 0;
pform.iPos = 0;
//file name
sOutFileNameString = sqlReader.GetString(0).Trim();
//file info
sFileInfo = sqlReader.GetString(1).Trim();
//file size
lFileSize = (long)sqlReader.GetInt32(2);
//flie ver
sFileVer = sqlReader.GetString(3).Trim();
//file full path
sOutFileString = sOutFilePathString + sOutFileNameString;
//Creat a file
fs = new FileStream(sOutFileString,FileMode.OpenOrCreate,FileAccess.Write);
bw = new BinaryWriter(fs);
//load file
lrltSize = sqlReader.GetBytes(4, lpointer, outbyte, 0, bufferSize);
lTotalLoadByte += lrltSize;
pform.iPos = (int)(lTotalLoadByte*100 / lFileSize);
pform.slbOutPut += "开始下载文件:" + sOutFileNameString + " .....\r\n";
while(lrltSize == bufferSize)
{
//未读完
System.Threading.Thread.Sleep(500);
bw.Write(outbyte);
bw.Flush();
lpointer += lrltSize;
lrltSize = sqlReader.GetBytes(4, lpointer, outbyte, 0, bufferSize);
lTotalLoadByte += lrltSize;
pform.iPos = (int)(lTotalLoadByte*100 / lFileSize);
if(pform.bAbort == true)
{
bw.Write(outbyte);
bw.Flush();
bw.Close();
fs.Close();
sqlConn.Close();
sqlReader.Close();
return false;
}
}
bw.Write(outbyte);
bw.Flush();
bw.Close();
fs.Close();
pform.iPos = 100 ;
Form1.static_Para.arrlst.Add(sOutFileNameString);
pform.slbOutPut += "文件:" +sOutFileNameString +"下载成功!\r\n";
}
sqlConn.Close();
sqlReader.Close();
return true;
}
catch(Exception ex)
{
sOutMessage = ex.Message;
pform.slbOutPut += sOutMessage + "\r\n";
return false;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -