New and Old platforms conversion data import Summary
A mobile company uses an old internal release platform to update the content of the old platform to the new platform.
ArticleData Import is mainly done by SQL statement insert. Different fields correspond to each other.
However, the problem lies in the attachment data of the old platform, which is saved as binary in the database.
The new publishing platform, the attachment is a normal file.
The number of attachments is not too large. More than one thousand, but manual attachments are enough.
I am a lazy, write a small Program To read binary data from the old database and write it to the directory of the files saved on the new platform.
Insert a data entry into the database of the new platform.
Main Code As follows: // Read and Write files from the database
// I wrote the first code on the Internet, but later I found that the execution efficiency was very low. Failed!
While (Dr. Read ())
{
FS = New Filestream (filename, filemode. createnew );
BW = New Binarywriter (FS );
Startindex = 0 ;
Retval = Dr. getbytes ( 2 , Startindex, outbyte, 0 , Buffersize );
While (Retval = Buffersize)
{
Bw. Write (outbyte );
Bw. Flush ();
Startindex + = Buffersize;
Retval = Dr. getbytes ( 2 , Startindex, outbyte, 0 , Buffersize );
}
Bw. Write (outbyte, 0 ,( Int ) Retval - 1 );
Bw. Flush ();
}
// Modified code, OK
While (Dr. Read ())
{
Byte [] TMP;
TMP = ( Byte []) Dr [ " Encls " ];
Bw. Write (TMP );
}
// Writes c: \ test. rmvb to the database, which is a large file used for testing efficiency.
Private Void Button2_click ( Object Sender, system. eventargs E)
{
Sqlconnection con = New Sqlconnection ( " Server = (local); uid = sa; Pwd =; database = NETServer " );
Sqldataadapter da = New Sqldataadapter ( " Select * From DBO. document_appendix " , Con );
Sqlcommandbuilder mycb = New Sqlcommandbuilder (DA );
Dataset DS = New Dataset ( " Myimages " );
Da. missingschemaaction = Missingschemaaction. addwithkey;
Filestream FS = New Filestream ( @" C: \ test. rmvb " , Filemode. openorcreate, fileaccess. Read );
Byte [] Mydata = New Byte [Fs. Length];
FS. Read (mydata, 0 , System. Convert. toint32 (FS. Length ));
FS. Close ();
Da. Fill (DS, " Myimages " );
Datarow myrow;
Myrow = DS. Tables [ " Myimages " ]. Newrow ();
Myrow [ " Filename " ] = " Test. rmvb " ;
Myrow [ " Encls " ] = Mydata;
DS. Tables [ " Myimages " ]. Rows. Add (myrow );
Da. Update (DS, " Myimages " );
Con. Close ();
MessageBox. Show ( " Done! Congratulation! " );
}