日期:2014-05-16 浏览次数:20316 次
CREATE TABLE [SBLOB] ( [CabData] [image] NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO INSERT INTO [SBLOB] ([CabData]) SELECT BulkColumn FROM OPENROWSET( Bulk 'C:\MyCab.zip', SINGLE_BLOB) AS BLOB
int IndexBlobContents = 0; sqlconn.ConnectionString = "Server=" + ServerName + ";UID=" + UID + ";Pwd=" + Pwd + ";Database=" + DBName; sqlconn.Open(); Blobsize = 10000000;// Initalize the BlobSize cmd = new SqlCommand("SELECT " + Fldnames + " FROM " + TbName + " " + Cndt, sqlconn); sqlDr = cmd.ExecuteReader(); while (sqlDr.Read()) { //dirname + filename ExtractFileName = ExtractPath + sqlDr[1].ToString(); startIndex = 0; // Reset the starting byte for the new BLOB. outBuffer = new byte[Blobsize]; if (File.Exists(@ExtractFileName)) { File.Delete(@ExtractFileName); } // Create a file to hold the output. fs = new FileStream(@ExtractFileName, FileMode.OpenOrCreate, FileAccess.Write); bw = new BinaryWriter(fs); // Read bytes into outByte[] and retain the number of bytes returned. blob = sqlDr.GetBytes(IndexBlobContents, startIndex, outBuffer, 0, Blobsize); while (blob == Blobsize) // Continue while there are bytes beyond the size of the buffer. { bw.Write(outBuffer); bw.Flush(); startIndex += Blobsize; blob = sqlDr.GetBytes(IndexBlobContents, startIndex, outBuffer, 0, Blobsize); } // Write the remaining buffer. bw.Write(outBuffer, 0, (int)blob); bw.Flush(); bw.Close(); fs.Close(); } sqlDr.Close();