Basic Idea: open an image in the form of a binary stream and read and retrieve the binary code and store it in the database.
Filestream PIC = new filestream (sfilename, filemode. Open); // sfilename is the image path
Byte [] picbyte = new byte [pic. Length];
PIC. Read (picbyte, 0, picbyte. Length );
0. My test example is as follows:
String strfilename = @ "E: \ Documents ents and Settings \ jhtest \ My Documents ents \ my pictures \ 2.jpg ";
Filestream PIC = new filestream (strfilename, filemode. Open); // sfilename is the image path
Byte [] picbyte = new byte [pic. Length];
PIC. Read (picbyte, 0, picbyte. Length );
Sqlconnection con = new sqlconnection ("database = jdplan; server = jhtest2; uid = sa; Pwd = jhtest123 ;");
String plain text = "insert into test (fileformat, imageformat) values (@ image, @ file )";
Sqlcommand cmd = new sqlcommand (plain text, con );
// Of course, binary values cannot be input in the form of string addition. They can only be input in the form of adding parameters.
Cmd. Parameters. Add ("@ image", sqldbtype. Image, convert. toint32 (PIC. Length). value = picbyte;
Cmd. Parameters. Add ("@ file", sqldbtype. Binary, convert. toint32 (PIC. Length). value = picbyte;
Cmd. commandtype = commandtype. text;
Con. open ();
Int returnvalue = cmd. executenonquery ();
Con. Close ();
If (returnvalue> 0)
{
Console. writeline ("insert sucess !!! ");
Console. Readline ();
}
1. The winform instance is as follows:
Openfiledialog openfiledialog1 = new openfiledialog ();
Openfiledialog1.initialdirectory = "C :\\";
Openfiledialog1.filter = "BMP file (*. BMP) | *. BMP | JPG file (*. JPG) | *. JPG | GIF file (*. GIF) | *. GIF | TIFF File (*. tiff) | *. tiff | WMF file (*. WMF) | *. WMF ";
Openfiledialog1.filterindex = 1;
Openfiledialog1.title = "open image file ";
Openfiledialog1.restoredirectory = true;
If (openfiledialog1.showdialog () = dialogresult. OK)
{
String strfilename = openfiledialog1.filename;
Bitmap m_bitmap = new Bitmap (strfilename );
This. picturebox1.image = m_bitmap;
}
}
// Save the picturebox1 image to dataset1
Private void savepictodataset (currencymanager thispic)
{
// If (this. picturebox1.image .! = NULL)
Memorystream MS = new memorystream ();
This. picturebox1.image. Save (MS, system. Drawing. imaging. imageformat. BMP );
Byte [] mydata = new byte [Ms. Length];
Ms. Position = 0;
Ms. Read (mydata, 0, convert. toint32 (Ms. Length ));
Datarow DRV = (datarowview) thispic. Current). row;
DRV ["pic"] = mydata;
2. the ASP. NET instance is as follows:
ASP. How to upload images to the database under. NET and read the images first, create an image storage database table in SQL Server. imagedata column is the binary data storage field of the image, imagecontenttype column is the record field of the image file type, imagedescription column is the description field of the saved image file, and imagesize column is the length field of the saved image file. The structure is as follows: Create Table [DBO]. [imagestore] ([imageid] [int] identity (1, 1) not null, [imagedata] [Image] Null, [imagecontenttype] [varchar] (50) Collate chinese_prc_ci_as null, [image description] [varchar] (200) Collate chinese_prc _ Ci_as null, [imagesize] [int] Null) on [primary] textimage_on [primary] * // uploadimage. the content of the aspx program is as follows: <% @ page inherits = "uploadimage. uploadimage "src =" uploadimage. CS "Language =" C # "%> <HTML> <title> upload an image </title> <body bgcolor =" # ffffff "> <form enctype =" multipart/form- data "runat =" server "id =" form1 "> <Table runat =" server "width =" 700 "align =" Left "id =" Table1 "cellpadding =" 0" cellspacing = "0" border = "0"> <tr> <TD> upload Image (select the image you want to upload) </TD> <input type = "file" id = "up_file" runat = "server" style = "width: 320 "Accept =" text/* "name =" up_file "> </TD> </tr> <TD> file description (add upload image description, for example: author, source) </TD> <asp: textbox runat = "server" width = "239" id = "txtdescription" maintainstate = "false"/> </TD> </tr> <TD> <asp: label runat = "server" id = "txtmessage" forecolor = "red" maintainstate = "false"/> </TD> <asp: button runat = "server" width = "239" oncl Ick = "button_submit" text = "uploadimage"/> </TD> </tr> </table> </form> </body>