/// <summary>
/// Сохранить файл в базу данных
/// </summary>
public static void StoreFile(string connString, DateTime startDate, DateTime endDate, string site, string fileName)
{
if(site == null)
throw new ArgumentNullException("site");
if(fileName == null)
throw new ArgumentNullException("fileName");
if(!File.Exists(fileName))
throw new FileNotFoundException("File not found.", fileName);
SqlParameter vStartDate = new SqlParameter(PARM_START_DATE, SqlDbType.DateTime);
vStartDate.IsNullable = true;
vStartDate.Value = (startDate != DateTime.MinValue) ? (object)startDate : (object)DBNull.Value;
SqlParameter vEndDate = new SqlParameter(PARM_END_DATE, SqlDbType.DateTime);
vEndDate.Value = endDate;
SqlParameter vSite = new SqlParameter(PARM_SITE, SqlDbType.VarChar);
vSite.Value = site;
Byte[] blob = null;
using(FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
blob = new Byte[fileStream.Length];
fileStream.Read(blob, 0, blob.Length);
}
SqlParameter vFile = new SqlParameter(PARM_FILE, SqlDbType.Image, blob.Length);
vFile.Value = blob;
SqlParameter[] parms = new SqlParameter[]
{
vStartDate,
vEndDate,
vSite,
vFile,
};
int timeout = 10800;
SqlHelper.ExecuteNonQuery(connString, timeout, CommandType.Text, SQL_STORE_FILE, parms);
}
private const string SQL_STORE_FILE = "INSERT INTO Archive (StartDate, EndDate, Site, [File]) VALUES(@vStartDate, @vEndDate, @vSite, @vFile)";
private const string SQL_DOWNLOAD_FILE = "select [File] from Archive WHERE Id = @vId";
private const string PARM_START_DATE = "@vStartDate";
private const string PARM_END_DATE = "@vEndDate";
private const string PARM_SITE = "@vSite";
private const string PARM_FILE = "@vFile";
private const string PARM_ID = "@vId";
p
public static void DownloadLogFile(string connString, int fileId, string fileName)
{
SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);
parm.Value = fileId;
SqlParameter[] parms = new SqlParameter[]{parm,};
using(SqlDataReader reader = SqlHelper.ExecuteReader(connString, CommandType.Text, SQL_DOWNLOAD_FILE, parms))
{
if(reader.Read())
{
if(File.Exists(fileName))
File.Delete(fileName);
using(FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
using(BinaryWriter bw = new BinaryWriter(stream))
{
int bufferSize = 1024 * 1024 * 3;
byte[] buffer = new byte[bufferSize];
long startIndex = 0;
int columnIdx = 0;
long retval = reader.GetBytes(columnIdx, startIndex, buffer, 0, bufferSize);
while(retval == bufferSize)
{
bw.Write(buffer);
bw.Flush();
startIndex += bufferSize;
retval = reader.GetBytes(columnIdx, startIndex, buffer, 0, bufferSize);
}
bw.Write(buffer, 0, (int)retval);
bw.Flush();
}
}
else
{
throw new WASException("File in dataBase not found.", Helper.FormatString("fileId = {0}", fileId));
}
}
}