|
我正在尝试将一个约600 MB的zip文件上传到SQL 2008
2 H1 b: J6 i+ ~FILESTREAM表,但出现OutOfMemoryException。我使用一个SqlFileStream类上传的文件(在本教程中描述-1 [2 B' [9 T* K; c1 _
http://www.aghausman.net/dotnet/saving-and-retrieving-file-using-filestream-# r/ |( r: l( f1 t& i# q; G' u5 f
sql-server-2008.html)
0 j6 T: H1 p; M) t9 f。如果有问题,我有一台具有4GB内存的32位Vista计算机,并且正在使用VS 2010,Entity Framework 4。; T! |# j( D0 p! t( B2 t
这是我的代码段-/ g& i( [8 x0 b3 @ ?( I- }
public static void AddItem(RepositoryFile repository)
" N" P; m' |0 Q& {8 `* j {
; C) r% l4 g) t/ V0 _ var contents = repository.Data; // I get the exception at this line.
+ C- G; o m2 s& W) I repository.Data = System.Text.Encoding.ASCII.GetBytes("0x00");
- K3 q( N) t. O7 [ using (var scope = new TransactionScope())
) h+ `2 `' W- Y# ] {
- F1 S0 i7 f5 r( {0 T4 X using (var db = new MyEntities())2 G9 i/ H* O8 `1 h
{ k4 p# k0 f/ B/ |7 o
db.RepositoryTable.AddObject(repository);% P, }8 R- W5 T8 O& J2 X
db.SaveChanges();
% |+ b7 m( m9 [9 n }
- A9 t. {/ V ^3 ? using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))# ^. J5 u9 {: A% J# W9 ^$ |
using (var cmd = new SqlCommand("SELECT Data.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM dbo.RepositoryTable", con))
# e! A E# K5 L/ W0 Z9 c {
9 H; r0 Z5 v4 |5 B# b2 \+ V cmd.Connection.Open();8 x i8 k3 s% ?6 E( X8 `
using (var reader = cmd.ExecuteReader()). `) q7 h$ S- r" H& q
{6 a0 B( f+ N8 M
while (reader.Read())/ R \" e# _# M5 v( N
{9 k- r# m: N, \' ~" s' c: {
var path = reader.GetString(0);& k2 b2 J; s( ~/ r4 h4 {& ?
var transactionContext = reader.GetSqlBytes(1).Buffer;6 e2 `4 \: V) M2 V
var fileStream = new SqlFileStream(path, transactionContext, FileAccess.Write);/ ]# G# Y$ D2 D
fileStream.Write(contents, 0, contents.Length);. r; J* D- b$ e# w, ?
fileStream.Close(); f2 v# q+ Z4 Q! c7 Z
}6 q+ [8 ~5 l! ?9 z( k
}
$ |9 v! J6 ^; B9 J7 X8 s }2 S: O% }1 W0 I. s& Z, O/ _
scope.Complete();$ ~, J) u; G6 b! T
}
; f1 W4 p: b. O) u }4 C6 T) E, u* v
如何上传文件而没有任何错误?9 R* P# V9 _) {
谢谢!( n. O9 D9 [6 Z# Z3 q* [; J& S
; ]. p q, ~' f$ c解决方案:
9 k8 G1 @0 d, u! F $ @; I% H; |1 A. S# m+ R; a8 G4 p0 c
! f" |! |+ {7 S8 e
3 ]9 u* n7 Q* U @4 b5 q! v: `- _# c 我发现了问题。这是我的代码-
) f) f% V; [7 f& hprivate void AddFile()3 y5 `8 }& W/ h M
{6 w9 b& R! ]& j, C% P l& H3 g; ^
if (!fupFile.HasFile)
j( o" V% _% H0 H {
_2 Q. U% n p5 A' \ lblMessage.Text = "lease select a file."; * n! |" K* B3 Q% H
return;
# ~1 Q: h/ c/ J4 [* ? }
3 g+ L1 W |$ T var data = new byte[(int) fupFile.FileContent.Length];
5 c3 w+ A# V, F2 ?6 x' `: U fupFile.FileContent.Read(data, 0, data.Length);
6 r# C! s; N, h' n if (fupFile.FileContent.Length > 0)
5 _/ N1 }4 n- [4 i; I {" _$ x; ?4 Q6 E3 c N
var repositoryFile = new Repository8 Y3 I/ [( k. P. |
{, P L# K* j. y: q; ?9 D8 S
ID = Guid.NewGuid(),
2 F, m e7 q4 M% X Name = Path.GetFileName(fupFile.PostedFile.FileName), " A9 S* U: x0 I) c
Data = System.Text.Encoding.ASCII.GetBytes("0x00")$ C( C; T0 f' f( o
};( v9 }7 e' K& r+ q8 J2 X
RepositoryController.AddItem(repositoryFile, data); // Calling DAL class.: ^7 t# G. U: n P) h9 ~
}
7 f% l1 ?% [; A; M( b }
0 |) P8 z7 \6 b; `// DAL method/ ~7 k p" {0 x- U. r! F9 D
public static void AddItem(RepositoryFile repository, byte[] data)& |# Z8 v* x6 n0 ]0 r1 k
{
& B7 e. J# c$ E& f4 H" ^ using (var scope = new TransactionScope())
! b5 x3 B, K+ K. \% z1 D5 m {
$ P; P; i9 L1 D8 W3 ^ using (var db = new MyEntities()) // DBContext( c" _! S5 q" v+ z: G
{
& t* a+ V! g" ^ }1 U, o db.RepositoryTable.AddObject(repository);
8 o6 [2 N: `" @+ P( i db.SaveChanges();. O9 N) k% q* L; F0 z
}( Z$ b% x" b; o2 X. P2 F% z
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))2 a: l: R/ D/ w$ o1 M9 E
using (var cmd = new SqlCommand(string.Format("SELECT Data.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM dbo.RepositoryTable WHERE ID='{0}'", repository.ID), con)) // "Data" is the column name which has the FILESTREAM. Data.PathName() gives me the local path to the file.
. ?/ W: u4 i' _1 E! M% D4 s {# k7 ]9 W( I1 u" x! p- D
cmd.Connection.Open();0 Z0 S9 O+ k- s4 R6 M. V% y* g( j
using (var reader = cmd.ExecuteReader())* ~4 H* e* I- o
{
6 m5 [& @6 g) [ while (reader.Read())/ r: u. G' _% n. [
{
( F- u. N& [2 ^; _4 a4 Q var path = reader.GetString(0);( d' g& H" g& U0 K7 l; ?8 J2 w" W; a) `
var transactionContext = reader.GetSqlBytes(1).Buffer;1 @% Z$ a! E- E# F4 E7 ^
var fileStream = new SqlFileStream(path, transactionContext, FileAccess.Write);
5 F/ B5 m8 Y8 \- @# i fileStream.Write(contents, 0, contents.Length); //Write contents to the file.
) E0 x5 X6 h- ~3 P/ M3 p3 G( H fileStream.Close();
+ Y- K& M8 D6 L. _. A# T% W }
2 C1 k+ k( S. e: k4 T" z: s }' x4 t' Z, D, E. h7 \
}8 L2 I% ]8 l* @- Z+ M
scope.Complete();
2 _! W$ C5 Q( Z# }0 q. ` }# b+ ^2 U: Y& F! n, g9 N; U8 V) p
} |
|