1. Convert string to binary array
String content = "this is a test! ";
System. Text. unicodeencoding converter = new system. Text. unicodeencoding ();
Byte [] bytearr = converter. getbytes (content );
2. Convert the binary array into a string
System. Text. unicodeencoding converter = new system. Text. unicodeencoding ();
String spcontent = converter. getstring (bytearr );
[Files are saved to the database in binary format]
During compilation, files are saved to the database using binary data. For example, you can save the "C: \ test.html" file to the database and read it out:
1. Save the file as a stream to the database:
Int ITAG = 0;
String content = "";
Stringbuilder sb = new stringbuilder ();
String filename = @ "C: \ test.html ";
Streamreader objreader = new streamreader (filename, system. Text. encoding. getencoding ("gb2312 "));
String Sline = "";
While (Sline! = NULL)
{
Sline = objreader. Readline ();
If (Sline! = NULL)
{// You can perform corresponding processing here, such as filtering data in the file.
SB. append (Sline );
}
}
Objreader. Close ();
Content = sb. tostring (); // if you want to display the content of the entire file on the interface, you can use <% = content %> to place it in the corresponding place.
System. Text. unicodeencoding converter = new system. Text. unicodeencoding ();
Byte [] bytearr = converter. getbytes (content );
// The following code is inserted into the database,
Strinsertcmd = "insert into document (partition entid, documentcontent, addtime, moditime, status) values ('" + partition entid + "',?, '"+ Nowtime +"', '"+ nowtime +"', '00 ')";
Cmd = new oledbcommand (strinsertcm, connectionobj );
Param = new oledbparameter ("documentcontent", oledbtype. varbinary, bytearr. length, parameterdirection. Input, false, 0, 0, null, datarowversion. Current, bytearr );
Cmd. Parameters. Add (PARAM );
ITAG = cmd. executenonquery ();
If (ITAG> 0) {// success !}
2. Reading and saving as a file or string from the database is the opposite of step 1.
1. Convert gb2312 data to UTF-8 data as follows (other codes and so on ):
Public String gb2312toutf8 (string ssourse ){
String utf8_info = string. empty;
Encoding utf8 = encoding. utf8;
Encoding gb2312 = encoding. getencoding ("gb2312 ");
Byte [] unicodebytes = gb2312.getbytes (ssourse );
Byte [] asciibytes = encoding. Convert (gb2312, utf8, unicodebytes );
Char [] asciichars = new char [utf8.getcharcount (asciibytes, 0, asciibytes. Length)];
Utf8.getchars (asciibytes, 0, asciibytes. length, asciichars, 0 );
Utf8_info = new string (asciichars );
Return utf8_info;
}