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 stringCopyCodeThe Code is as follows: system. Text. unicodeencoding converter = new system. Text. unicodeencoding ();
String spcontent = converter. getstring (bytearr );
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:Copy codeThe Code is as follows: 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 encodings and so on): copy Code the code is as follows: 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;
}