Я пытаюсь разделить эту функциональность на традиционный трехуровневый шаблон, но с некоторыми трудностями.Разделение кода доступа к данным на отдельный уровень
Доступ к данным - это не то, с чем я работал много, и мне хотелось бы получить некоторые рекомендации.
Я начал с code from this blog, и то, что я сделал до сих пор, ниже. Я конвертирую возвращаемый тип в SqlDataReader
, и на данный момент я прокомментировал код, который нужно разделить.
// This is the DAL layer:
public SqlDataReader DownloadFile(int fileId)
{
//int id = int.Parse((sender as LinkButton).CommandArgument);
//byte[] bytes;
//string fileName, contentType;
//string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
//using (SqlConnection con = new SqlConnection(constr))
//{
cmd.CommandText = "SELECT [fileId],[fileName],[fileData],[postedBy] FROM [dbo].[FilesLibrary] where [email protected]";
cmd.Parameters.AddWithValue("@Id", fileId);
cmd.Connection = cmd.Connection;
try
{
cmd.Connection.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
//sdr.Read();
//bytes = (byte[])sdr["Data"];
//contentType = sdr["ContentType"].ToString();
//fileName = sdr["Name"].ToString();
return sdr;
}
}
catch (Exception ex)
{
cmd.Connection.Close();
throw;
}
//}
//Response.Clear();
//Response.Buffer = true;
//Response.Charset = "";
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.ContentType = contentType;
//Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
//Response.BinaryWrite(bytes);
//Response.Flush();
//Response.End();
}
// The BL is below:
public SqlDataReader GetFileDownload(int fileId)
{
try
{
dsGetFiles files = new dsGetFiles();
return files.DownloadFile(fileId);
}
catch (Exception ex) { throw ex; }
}
// The code file is as follows:
protected void DownloadFile(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
GetFiles fileInfo = new GetFiles();
fileInfo.GetFileDownload(id);
// Here I don't know what to do with the fileInfo object and how to get data out of it.
//sdr.Read();
//bytes = (byte[])sdr["fileData"];
//contentType = sdr["ContentType"].ToString();
//fileName = sdr["fileName"].ToString();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
Скажите, если что-то непонятно! – mybirthname