Using system; Using system. drawing; Using system. collections; Using system. componentmodel; Using system. Windows. forms; Using system. Data; Using system. IO; Using system. Data. sqlclient;Namespace windowsapplication21 { /// <Summary> /// Summary of form1. /// </Summary> Public class form1: system. Windows. Forms. Form { Private system. Windows. Forms. Button button1; /// <Summary> /// Required designer variables. /// </Summary> Private system. componentmodel. Container components = NULL; Private string connectionstring = "Integrated Security = sspi; initial catalog =; Data Source = localhost ;"; Private sqlconnection conn = NULL; Private sqlcommand cmd = NULL; Private system. Windows. Forms. Button button2; Private system. Windows. Forms. picturebox pic1; Private system. Windows. Forms. openfiledialog openfiledialog1; Private string SQL = NULL; Private system. Windows. Forms. Label label2; Private string nowid = NULL; Public form1 () { // // Required for Windows Form Designer support // Initializecomponent (); Conn = new sqlconnection (connectionstring ); // // Todo: add Any constructor code after initializecomponent calls // } /// <Summary> /// Clear all resources in use. /// </Summary> Protected override void dispose (bool disposing) { If (conn. State = connectionstate. open) Conn. Close (); If (disposing) { If (components! = NULL) { Components. Dispose (); } } Base. Dispose (disposing ); } # Region windows Form Designer generated code /// <Summary> /// The designer supports the required methods-do not use the code editor to modify /// Content of this method. /// </Summary> Private void initializecomponent () { This. button1 = new system. Windows. Forms. Button (); This. pic1 = new system. Windows. Forms. picturebox (); This. button2 = new system. Windows. Forms. Button (); This. openfiledialog1 = new system. Windows. Forms. openfiledialog (); This. label2 = new system. Windows. Forms. Label (); This. suspendlayout (); // // Button1 // This. button1.location = new system. Drawing. Point (0, 40 ); This. button1.name = "button1 "; This. button1.size = new system. Drawing. Size (264, 48 ); This. button1.tabindex = 0; This. button1.text = "Add new image "; This. button1.click + = new system. eventhandler (this. button#click ); // // Pic1 // This. pic1.location = new system. Drawing. Point (280, 8 ); This. pic1.name = "pic1 "; This. pic1.size = new system. Drawing. Size (344,264 ); This. pic1.tabindex = 3; This. pic1.tabstop = false; // // Button2 // This. button2.location = new system. Drawing. Point (0,104 ); This. button2.name = "button2 "; This. button2.size = new system. Drawing. Size (264, 40 ); This. button2.tabindex = 4; This. button2.text = "Recovering images from the Database "; This. button2.click + = new system. eventhandler (this. button2_click ); // // Openfiledialog1 // This. openfiledialog1.filter = "/" image file (*. jpg, *. BMP, *. GIF) | *. jpg | *. BMP | *. gif /""; // // Label2 // This. label2.location = new system. Drawing. Point (0,152 ); This. label2.name = "label2 "; This. label2.size = new system. Drawing. Size (264, 48 ); This. label2.tabindex = 5; // // Form1 // This. autoscalebasesize = new system. Drawing. Size (6, 14 ); This. clientsize = new system. Drawing. Size (632,273 ); This. Controls. addrange (new system. Windows. Forms. Control [] { This. label2, This. button2, This. pic1, This. button1 }); This. Name = "form1 "; This. Text = "form1 "; This. Load + = new system. eventhandler (this. form#load ); This. resumelayout (false ); } # Endregion /// <Summary> /// Main entry point of the application. /// </Summary> [Stathread] Static void main () { Application. Run (New form1 ()); } Private void button#click (Object sender, system. eventargs E) { Openfiledialog1.showdialog (); If (openfiledialog1.filename. Trim ()! = "") { Fileinfo Fi = new fileinfo (openfiledialog1.filename ); String imgtitle = openfiledialog1.filename; Int imgdatalen = (INT) Fi. length; Byte [] imgdata = new byte [imgdatalen]; Stream imgdatastream = Fi. openread (); Int n = imgdatastream. Read (imgdata, 0, imgdatalen ); If (conn. State = connectionstate. open) Conn. Close (); Connectionstring = "Integrated Security = sspi;" + "Initial catalog = mydb;" + "Data Source = localhost ;"; Conn. connectionstring = connectionstring; Try { String myselectquery = "insert into imagestore (imgtitle, imgdata) values (@ imgtitle, @ imgdata )"; // String myselectquery = "Update imagestore set imgtitle = @ imgtitle, imgdata = @ imgdata "; Sqlcommand mycommand = new sqlcommand (myselectquery, Conn ); Sqlparameter paramtitle = new sqlparameter ("@ imgtitle", sqldbtype. varchar, 50 ); Paramtitle. value = imgtitle; Mycommand. Parameters. Add (paramtitle ); Sqlparameter paramdata = new sqlparameter ("@ imgdata", sqldbtype. Image ); Paramdata. value = imgdata; Mycommand. Parameters. Add (paramdata ); Conn. open (); Int numrowsaffected = mycommand. executenonquery (); Conn. Close (); } Catch (exception ERR) { MessageBox. Show ("your input name may already exist in the database or it is empty. Please check it! "+ Err. tostring ()); } Finally {} } } Private void form1_load (Object sender, system. eventargs E) { } Private void button2_click (Object sender, system. eventargs E) { // Open the database connection If (conn. State = connectionstate. open) Conn. Close (); Connectionstring = "Integrated Security = sspi;" + "Initial catalog = mydb;" + "Data Source = localhost ;"; Conn. connectionstring = connectionstring; // Create a data adapter String SQL = "select * From imagestore "; Sqlcommand command = new sqlcommand (SQL, Conn ); Try {Conn. open ();} Catch (exception newerr) { MessageBox. Show ("data connection cannot be enabled! "); } Finally {} Sqldatareader DR = command. executereader (); If (dr. Read ()) { Fileinfo Fi = new fileinfo ("Temp "); Filestream mystream = Fi. Open (filemode. Create ); Byte [] mydata = (byte []) Dr ["imgdata"]); // Label2.text = "what you see now:" + Dr ["imgtitle"]. tostring (); Foreach (byte A in mydata) { Mystream. writebyte (); } Mystream. Close (); Image myimage = image. fromfile ("Temp "); Pic1.image = myimage; Pic1.refresh (); Dr. Close (); } Else { MessageBox. Show ("data is not read successfully! "); } Conn. Close (); } } } |