To prevent injection and other problems, convert the text to Unicode or UTF8 for saving.
/// <Summary>
/// Convert a text string to a binary string with "," Separator
/// </Summary>
/// <Param name = "strContent"> text string </param>
/// <Returns> binary string with and separated by signs </returns>
Private string strTextTostrBin (string strText)
{
Byte [] bytearr = null;
String stringtobin = "";
System. Text. Encoding encoding = System. Text. Encoding. UTF8;
Bytearr = encoding. GetBytes (strText );
For (int I = 0; I <bytearr. Length; I ++)
{
Stringtobin + = "," + bytearr [I]. ToString ();
}
Return stringtobin. Substring (1 );
}
/// <Summary>
/// Convert a binary string separated by commas (,) into a text string
/// </Summary>
/// <Param name = "strBin"> binary string with and separated by signs </param>
/// <Returns> text string </returns>
Private string strBinTostrText (string strBin)
{
String [] bintostr = strBin. Split (',');
Array binArray = Array. CreateInstance (Type. GetType ("System. Byte"), bintostr. Length );
For (int I = binArray. GetLowerBound (0); I <= binArray. GetUpperBound (0); I ++)
{
BinArray. SetValue (byte. Parse (bintostr [I] + ""), I );
}
Byte [] strtobin = new byte [bintostr. Length];
For (int I = binArray. GetLowerBound (0); I <= binArray. GetUpperBound (0); I ++)
{
Strtobin [I] = (byte) binArray. GetValue (I );
}
System. Text. Encoding encoding = System. Text. Encoding. UTF8;
Return encoding. GetString (strtobin );
}
C # How to access images
Write image files, use the File Upload control to read data into the stream, and then write it into a two-byte array, and directly write it into the database.
Private void ImgDataReadWrite ()
{
HttpPostedFile UpFile = UP_File.PostedFile; // HttpPostedFile object, used to read image file attributes
FileLength = UpFile. ContentLength;
Try
{
If (FileLength = 0)
{
LblMessage. Text = "<B> select the file to be uploaded </B> ";
}
Else
{
Byte [] FileByteArray = new byte [FileLength]; // temporary storage of Byte Arrays for image files
Stream StreamObj = UpFile. InputStream; // create a data Stream object
// Or Stream myStream = openFileDialog1.OpenFile ();
// Read image file data. FileByteArray is the data storage body, 0 is the Data Pointer position, and filelne is the data length.
StreamObj. Read (FileByteArray, 0, FileLength );
SqlConnection Con = new SqlConnection (System. Configuration. ConfigurationSettings. etettings ["ConnectionString"]);
String SqlCmd = "insert into ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@ Image, @ ContentType, @ ImageDescription, @ ImageSize )";
SqlCommand CmdObj = new SqlCommand (SqlCmd, Con );
CmdObj. Parameters. Add ("@ Image", SqlDbType. Binary, FileLength). Value = FileByteArray;
CmdObj. Parameters. Add ("@ ContentType", SqlDbType. VarChar, 50). Value = UpFile. ContentType; // record file type
// Upload other single table data records
CmdObj. Parameters. Add ("@ ImageDescription", SqlDbType. VarChar, 200). Value = txtDescription. Text;
// Record the file length, which is used for reading
CmdObj. Parameters. Add ("@ ImageSize", SqlDbType. BigInt, 8). Value = UpFile. ContentLength;
Con. Open ();
CmdObj. ExecuteNonQuery ();
Con. Close ();
LblMessage. Text = "<p> <B> OK! You have successfully uploaded your image </B> "; // a message indicating that the image has been uploaded successfully
}
}
Catch (Exception ex)
{
LblMessage. Text = ex. Message. ToString ();
}
}
Reads image files and writes them directly to the stream.
Private void ImgDataRead ()
{
Int ImgID = Convert. ToInt32 (Request. QueryString ["id"]);
SqlConnection Con = new SqlConnection (System. Configuration. ConfigurationSettings. etettings ["ConnectionString"]);
String SqlCmd = "SELECT * FROM ImageStore where id = @ ImageID ";
SqlCommand CmdObj = new SqlCommand (SqlCmd, Con );
CmdObj. Parameters. Add ("@ ImageID", SqlDbType. Int). Value = ImgID;
Con. Open ();
SqlDataReader SqlReader = CmdObj. ExecuteReader ();
SqlReader. Read ();
Response. ContentType = (string) SqlReader ["ImageContentType"]; // set the output file type
// Binary number of output image files
Response. OutputStream. Write (byte []) SqlReader ["ImageData"], 0, Convert. ToInt32 (SqlReader ["ImageSize"]);
Response. BufferOutput = true;
// Or byte [] bytes = (byte []) SqlReader ["ImageData"];
// MemoryStream memStream = new MemoryStream (bytes );
// Try
//{
// Bitmap myImage = new Bitmap (memStream );
// This. pictureBox1.Image = myImage;
//}
// Catch
//{
// This. pictureBox1.Image = null;
//}
Con. Close ();
}