For an asp.Net MVC project, I will need to process large files (mostly 200-300Mo, once 1Go).
I will store them in the database (for backup / consistency reasons).
I am concerned about the performance problem, so I want to avoid everything I can to have an array of bytes anywhere in the program, the goal is to work with the stream every time.
I have a multi-level application, which basically means that I have several "DataStore" that are responsible for connecting and receiving / inserting / updating data from the database.
Since EF does not support Filestream, I process the "part of the file" through simple Sql queries. I read a good article about using streams: http://blog.tallan.com/2011/08/22/using-sqlfilestream-with-c-to-access-sql-server-filestream-data/
And I have additional questions that I hope you can help me / point out a good direction:
- Since I have a layered application, as soon as I instantiate an SQLFileStream object, can I place the SqlCommand / Sql Connection / Transaction scope?
- If not, how should I close them?
- , , ASP. ASP.Net MVC, , ? , -
Stream.ToArray(), . , FileStreamResult, a Stream. ?
( , )
( , 50 .
, "read" - ( ):
SqlConnection conn = GetConnection();
conn.Open();
SqlCommand cmd = new SqlCommand(_selectMetaDataRequest, conn);
cmd.Parameters.Add(_idFile, SqlDbType.Int).Value = idFile;
SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();
string serverPath = rdr.GetSqlString(0).Value;
byte[] serverTxn = rdr.GetSqlBinary(1).Value;
rdr.Close();
return new SqlFileStream(serverPath, serverTxn, FileAccess.Read);
rdr.GetSqlBinary(1).Value, GET_FILESTREAM_TRANSACTION_CONTEXT null. , .
"TransactionScope" + .Complete();. .
BEGIN TRANSACTION, :
SqlConnection connection = GetConnection();
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "BEGIN TRANSACTION";
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.ExecuteNonQuery();
cmd = new SqlCommand(_selectMetaDataRequest, connection);
cmd.Parameters.Add(_idFile, SqlDbType.Int).Value = idFile;
SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();
string serverPath = rdr.GetSqlString(0).Value;
byte[] serverTxn = rdr.GetSqlBinary(1).Value;
rdr.Close();
SqlFileStream sqlFileStream = new SqlFileStream(serverPath, serverTxn, FileAccess.Read);
cmd = new SqlCommand();
cmd.CommandText = "COMMIT TRANSACTION";
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.ExecuteNonQuery();
"ExecuteNonQuery" "A transaction that was started in a MARS batch is still active at the end of the batch. The transaction is rolled back." FIRST-!