public FileModel GetFile(int id) { SqlConnection conn = GetConnection(); FileModel result = null; try { SqlCommand cmd = new SqlCommand( @"SELECT name, path, contentType, content FROM " + TableName + @" WHERE id = @id;", conn); //content_coding, DATALENGTH(content) as content_length, cmd.Parameters.Add("@id", SqlDbType.Int); cmd.Parameters["@id"].Value = id; SqlDataReader reader = cmd.ExecuteReader( CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow | CommandBehavior.CloseConnection); if (false == reader.Read()) { reader.Dispose(); conn = null; return(null); } string name = reader["name"].ToString(); string path = reader["path"].ToString(); string contentType = reader["ContentType"].ToString(); System.IO.Stream contentStream = new SqlReaderStream(reader, 3); result = new FileModel { Name = name, Path = path, ContentType = contentType, OutputStream = contentStream, }; conn = null; // ownership transfered to the reader/stream return(result); } finally { if (null != conn) { conn.Dispose(); } } }
public FileModel GetFile(int id) { SqlConnection conn = GetConnection(); FileModel result = null; try { SqlCommand cmd = new SqlCommand( @"SELECT name, path, contentType, content FROM "+TableName+@" WHERE id = @id;", conn); //content_coding, DATALENGTH(content) as content_length, cmd.Parameters.Add("@id", SqlDbType.Int); cmd.Parameters["@id"].Value = id; SqlDataReader reader = cmd.ExecuteReader( CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow | CommandBehavior.CloseConnection); if (false == reader.Read()) { reader.Dispose(); conn = null; return null; } string name = reader["name"].ToString(); string path = reader["path"].ToString(); string contentType = reader["ContentType"].ToString(); System.IO.Stream contentStream = new SqlReaderStream(reader, 3); result = new FileModel { Name = name, Path = path, ContentType = contentType, OutputStream = contentStream, }; conn = null; // ownership transfered to the reader/stream return result; } finally { if (null != conn) { conn.Dispose(); } } }