The picture is saved to data and read from the database (Winform, C # and VB.NET)

xiaoxiao2021-03-06  79

C # using system.drawing; using system.collections; using system.componentmodel; using system.data; using system.data.sqlclient; use system.io; using system.windows.forms;

Summary description of Namespace Regedit {///

/// Picture. /// public class Picture: System.Windows.Forms.Form {private System.Windows.Forms.OpenFileDialog openFileDlg; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button btnBrowse; private System.Windows.Forms.TextBox txtfile; private System.Windows.Forms.Button btnInsert; private System.Windows.Forms.TextBox txtname; private System.Windows.Forms.Button button1; private System.Windows.Forms.PictureBox pictureBox1; / // /// Design the variable required to design tools. /// private System.ComponentModel.Container components = null; SqlConnection conn = new SqlConnection ( "Data Source = localhost; Integrated Security = SSPI; Initial Catalog = mis"); public Picture () {// // Necessary items for Windows Form Design Tools // InitializeComponent ();

// // Todo: Add any constructive program code after the InitializeComponent call //}

///

/// Clear any resources in any use. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Design Tool Produced Program ///

// This is the method necessary for designing tool support - Do not use the program code editor to modify the /// this method.

/// private void InitializeComponent () {this.openFileDlg = new System.Windows.Forms.OpenFileDialog (); this.label1 = new System.Windows.Forms.Label (); this.txtfile = new System. Windows.Forms.TextBox (); this.btnBrowse = new System.Windows.Forms.Button (); this.btnInsert = new System.Windows.Forms.Button (); this.txtname = new System.Windows.Forms.TextBox (); This.Button1 = new system.windows.Forms.Button (); this.picturebox1 = new system.windows.Forms.Picturebox (); this.suspendlayout (); // // label1 // this.label1. Location = new system.drawing.point (16, 16); this.label1.name = "label1"; this.label1.size = new system.drawing.size (72, 23); this.label1.tabindex = 0; This.Label1.Text = "imagename"; // // txtfile // this.txtFile.Location = new system.drawing.point (88, 16); this.txtFile.name = "txtfile"; this.txtFile.size = New System.drawing.size (384, 22); this.txtFile.tabindex = 1; this.txtFile.text = ""; /// btnbrowse // this.btnbrowse.location = new system.drawing.point (24, 48); this.btnbrowse.name = "btnbrowse"; this.btnbrowse.tabindex = 2; this.btnbrowse.text = "browse"; this. Btnbrowse.click = new system.eventhandler (this.btnbrowse_click); // // btninsert // this.btninsert.location = new system.drawing.point (112, 48); this.btninsert.name = "btninsert"; THIS.BTNINSERT.TABINDEX = 3; this.btninsert.text = "insert"; this.btninsert.click = new system.eventhandler (this.btninsert_click);

// // txtname // this.txtname.location = new system.drawing.point (112, 88); this.txtname.name = "txtName"; this.txtname.tabindex = 4; this.txtname.text = " TXTNAME "; // // Button1 // this.button1.location = new system.drawing.point (24, 88); this.button1.name =" button1 "; this.button1.tabindex = 5; this.button1. Text = "Button1"; this.button1.click = new system.eventhandler (this.button1_click); // // picturebox1 // this.picturebox1.location = new system.drawing.point (24, 128); this. pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size (320, 160); this.pictureBox1.TabIndex = 6; this.pictureBox1.TabStop = false; // // Picture // this .Autoscalebasesize = new system.drawing.size (5, 15); this.clientsize = new system.drawing.size (488, 317); this.controls.add (this.PictureBox1); this.controls.add (this. Button1); this.Controls.add (this.txtname); this.controls.add (this.btninsert); this.controls.add (th Is.btnbrowse; this.txtfile; this.controls.add (this.label1); this.name = "picture"; this.text = "Operation picture"; this.Load = New System.EventHandler (this.Picture_Load); this.ResumeLayout (false);} #endregion

private void btnBrowse_Click (object sender, System.EventArgs e) {if (openFileDlg.ShowDialog () == DialogResult.OK) {txtfile.Text = openFileDlg.FileName; btnInsert.Enabled = true;}}

Private void btninsert_click (Object sender, system.eventargs e) {filestream fs = file.openread (txtfile.text); Byte [] content = new byte [fs.length]; fs.read (content, 0, content.length) Fs.close (); conn.open (); string sql = "INSERT INTO photos (Name, Photo) VALUES (@Name, @Photos)"; SQLCommand Comm = New SQLCommand (SQL, CONN); Comm.Parameters. Add ("@ photos", sqldbtype.image) .Value = content; comm.parameters.add ("@ name", sqldbtype.nvarchar) .value = txtname.text; if (Comm.executeNonQuery () == 1) { MessageBox.Show ( "Successfully insert image into database!");} else {MessageBox.Show ( "Failed to insert image into database");} conn.Close ();} private void Picture_Load (object sender, System.EventArgs e ) {}

Private void button1_click (object sender, system.eventargs e) {showimage (txtname.text);

Private void showimage (string s) {string str = "select photo from photos where name = '" s "'"; sqlcommand cmd = new sqlcommand (str, conn); conn.open (); Byte [] b = (Byte []) cmd.executescalar (); if (B.Length> 0) {MemoryStream Stream = New MemoryStream (B, True); Stream.write (B, 0, B.Length); DrawToscale (New Bitmap (stream )); Stream.close (); conn.close ();

Private void drawtoscale (image bmp) {picturebox1.image = new bitmap (bmp);

}} Vb.netimports system.iimports system.dataimports system.data.sqlclient

Public class photofrm inherits system.windows.forms.form Dim CN As SqlClient.sqlConnection

#REGION "WONDOWS FORM Design Tool Produced Project Code"

Public Sub new () mybase.new ()

'This is called for Windows Form Design Tools. InitializationComponent () 'Adds all initial settings after INITIALIZEComponent () call

End Sub

'Form overwriters Dispose to clear the component list. Protected overloads overrides sub dispose (byval disposing as boolean) ing disponation kiln (Components Is Nothing) Then components.dispose () end if endiffs) End sub

'Essential Components as System.com for Windows Form Design Tools Private Components As System.com

'Note: The following programs needed for Windows Form Design Tools' You can modify using the Windows Form Design Tool. 'Do not use the program editor to modify these programs.

Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Button1 As System. Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Public WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button Friend WithEvents Button5 As System.Windows.Forms.Button Private Sub InitializeComponent () Me.Label1 = New system.windows.forms.label me.label2 = new system.windows.Forms.Label me.textbox1 = new system.windows.Forms.TextBox Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog Me. SqlCommand1 = New System.Data.SqlClient.SqlCommand Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection Me.Button3 = New System.Windows.Forms.Button Me.Button4 = New System.Windows.Forms.Button Me.Button5 = New system.windows.forms.button me.suspendlayout () '' label1 'me.label1.location = new system.drawing.point (38, 15) me.label1.name = "label1"

Me.label1.size = new system.drawing.size (120, 21) me.label1.tabindex = 0 me.label1.text = "name" '' Label2 'me.label2.location = new system.drawing.point ( 38, 60) me.label2.name = "label2" me.label2.size = new system.drawing.size (120, 21) me.label2.tabindex = 1 me.label2.text = "photo" 'TEXTBOX1' Me.TextBox1.Location = new system.drawing.point (202, 15) Me.TextBox1.name = "textbox1" me.textbox1.size = new system.drawing.size (120, 21) me.textbox1.tabindex = 2 Me.TextBox1.Text = "" '' PictureBox1 'Me.PictureBox1.Location = New System.Drawing.Point (202, 52) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size (528, 187) Me.PictureBox1.tabindex = 3 me.picturebox1.tabstop = false '' Button1 'me.button1.location = new system.drawing.point (173, 284) Me.Button1.name = "Button1" me.button1.size = new system.drawing.size (90, 21) me.button1.tabindex = 4 me.button1.text = "brose" '' Button2 'me.button2. Location = new system.drawing.point (298, 284) me.button2.name = "Button2" me.button2.size = new system.drawing.size (90, 21) me.button2.tabindex = 5 me.button2. Text = "add" '' SQLCommand1 '

Me.SqlCommand1.CommandText = "dbo. [Sp_InsertPhoto]" Me.SqlCommand1.CommandType = System.Data.CommandType.StoredProcedure Me.SqlCommand1.Connection = Me.SqlConnection1 Me.SqlCommand1.Parameters.Add (New System.Data.SqlClient. Sqlparameter ("@ return_value", system.data.sqldbtype.int, 4, system.data.parameterDirection.ReturnValue, False, CType (0, Byte), CType (0, Byte), ", System.Data.DataRowVersion. Current, Nothing)) Me.SqlCommand1.Parameters.Add (New System.Data.SqlClient.SqlParameter ( "@ name", System.Data.SqlDbType.VarChar, 50)) Me.SqlCommand1.Parameters.Add (New System.Data .SqlClient.SqlParameter ( "@ image", System.Data.SqlDbType.VarBinary, 2147483647)) '' SqlConnection1 'Me.SqlConnection1.ConnectionString = "workstation id = HELLEN; packet size = 4096; user id = sa; data source = Hellen; Persist SECU "& _" RITY INFO = false; initial catalog = mis "'' Button3 'me.button3.location = new system.drawing.point (403, 284) Me.Button3.name = "Button3" me.button3.size = new system.drawing.size (90, 21) me.button3.tabindex = 6 me.button3.text = "show" '' Button4 'me. Button4.location = new system.drawing.point (614, 284) me.button4.name = "Button4" me.button4.size = new system.drawing.size (90, 21) me.button4.tabindex = 7 me. Button4.text = "close" '' button5 'me.button5.location =

New system.drawing.point (509, 284) me.button5.name = "button5" me.button5.size = new system.drawing.size (90, 21) me.button5.tabindex = 8 me.button5.text = "Delete" '' photofrm 'me.autoscalebasesize = new system.drawing.size (6, 14) me.clientsize = new system.drawing.size (796, 381) me.controls.add (me.button5) Me.Controls .Add (me.button4) me.controls.add (me.button3) me.controls.add (me.button2) me.controls.add (me.button1) me.controls.add (Me.PictureBox1) Me.Controls .Add (Me.TextBox1) me.controls.add (me.label2) me.controls.add (me.label1) me.name = "photofrm" me.text = "photofrm" me.ResumeLayout (false) End Sub

#End region

Private Sub photofrm_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'dim s as Decimal' If s = True Then 'MsgBox (Convert.ToString (Convert.ToUInt16 (s)))' End IF

End Sub

Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click

'Display Picture File OpenFiledialog1.initialDirectory = "D: / PIC" OpenFiledialog1.defaultext = "GIF" openfiledialog1.filter = "bmp files (*. Bmp) | * .bmp | gif files (*. Gif) | * .gif | JPG Files (*. Jpg) | * .jpg "OpenFiledialog1.Showdialog () PictureBox1.Image = Image.FromFile (OpenFiledialog1.FileName) End Sub

Private Sub Button2_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'To Insert Image Dim st As New FileStream (OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read) Dim s As String = TextBox1 .Text Dim Mbr as BinaryReader = New BinaryReader (St) DIM Buffer (st.Length) AS BYTE MBR.READ (Buffer, 0, Cint (St.Length)) st.close () InsertImage (Buffer, s)

End Sub 'Function For Inserting in the Procdeure in the Database Public Function InsertImage (ByRef buffer, ByVal str) cn = New SqlClient.SqlConnection (SqlConnection1.ConnectionString) cn.Open () Dim cmd As New SqlClient.SqlCommand ( "sp_InsertPhoto", cn) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.Add ( "@ name", SqlDbType.VarChar) .Value = TextBox1.Text cmd.Parameters.Add ( "@ image", SqlDbType.Image) .Value = buffer cmd .ExecuteNonQuery () MsgBox ( "Image inserted") cn.Close () End Function Private Sub ShowImage (ByVal s As String) cn = New SqlClient.SqlConnection (SqlConnection1.ConnectionString) cn.Open () Dim str As String = "SELECT Photo from photos where name = '"& s" "DIM CMD As New Sqlclient.sqlcommand (STR, CN) TextBox1.text = s Dim b () as byte b = cmd.executescalar () if (B.Length> 0) THEN DIM STREAM As New MemoryStream (B, True) Stream.Write (B, 0, B.Length) DrawToscale (New Bitmap (Stream) "stream.close () end if cn.close () End sub private subage PictureBox1.Image = New Bitmap BMP) End Subprivate Sub Button3_Click (Byval E AS System.EventArgs) Handles Button3.click

DIM I as string = inputbox ("please input name:") showimage (i) End Sub

Private sub button4_click (byvale as system.object, byval e as system.eventargs) Handles button4.click me.dispose ()

End Sub

Private Sub Button5_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click cn = New SqlClient.SqlConnection (SqlConnection1.ConnectionString) cn.Open () Dim s As String = InputBox ( "Please Input Name: ") DIM cmd as new sqlclient.sqlcommand (" delete from photos where name = '"& S &"' ", cmd.executenonquery () msgbox (" image deleted ") cn.close () end subsprivate sub photofrm_closing ( BYVAL Sender as object, byval e as system.componentmodel.canceleventargs) Handles mybase.closing

End Subend Class

转载请注明原文地址:https://www.9cbs.com/read-108309.html

New Post(0)