C # Save images in the database

Source: Internet
Author: User
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Data. oledb;
Using system. IO;

Namespace sendemail
{
Public partial class frmsaveimg: Form
{

Oledbconnection conn;
Public frmsaveimg ()
{
Initializecomponent ();
// Oledbconnection connection string
String strconn = @ "provider = Microsoft. Jet. oledb.4.0; Data Source =" + application. startuppath + "\ db1.mdb ";
// Create an oledbconnection object
Conn = new oledbconnection (strconn );
}

/// <Summary>
/// Execute SQL statement Functions
/// </Summary>
/// <Param name = "sqlcmd"> SQL statement </param>
/// <Param name = "paras"> parameter groups in SQL statements </param>
/// <Returns> return the number of affected records </returns>
Public int executesql (string sqlcmd, Params oledbparameter [] paras)
{
Oledbcommand cmd = new oledbcommand (sqlcmd, Conn );
If (conn. State = connectionstate. Closed)
{
Conn. open ();
}

Foreach (oledbparameter P in paras)
{
Cmd. Parameters. Add (P );
}

Int CNT = cmd. executenonquery ();
Conn. Close ();
Return CNT;
}

/// <Summary>
/// Execute SQL query
/// </Summary>
/// <Param name = "sqlcmd"> SQL statement </param>
/// <Returns> return data table </returns>
Public datatable querysql (string sqlcmd)
{
Oledbdataadapter ODA = new oledbdataadapter (sqlcmd, Conn );
Datatable dt = new datatable ();
ODA. Fill (DT );
Return DT;
}

// Click picturebox1 to execute
Private void picturebox#click (Object sender, eventargs E)
{
// Open the file dialog box
Openfiledialog ofd = new openfiledialog ();
// Select the image and click OK to load the image.
If (OFD. showdialog () = dialogresult. OK)
{
Picturebox1.imagelocation = ofd. filename;
}
}

// Click Save to save the image to the database.
Private void button#click (Object sender, eventargs E)
{
// Insert a data SQL statement. The IMG field is the field for storing images in the table (OLE type)
String SQL = "insert into tb_img (IMG) values (@ IMG )";

// Read the image file stream
Filestream FS = file. Open (picturebox1.imagelocation, filemode. Open, fileaccess. Read );
// Convert streams into byte Arrays
Byte [] mydata = new byte [fs. Length];
FS. Read (mydata, 0, mydata. Length );
FS. Close ();

// Assign values to the @ IMG parameter in the SQL statement
Oledbparameter P = new oledbparameter ("@ IMG", mydata );
// Execute an SQL statement to insert data into the table
Executesql (SQL, P );

// Refresh combox1
Combobox1.valuemember = "ID ";
Combobox1.displaymember = "ID ";
Combobox1.datasource = querysql ("select ID from tb_img ");
}

// Executed when the index of combox1 changes
Private void combobox#selectedindexchanged (Object sender, eventargs E)
{
// Obtain the record selected by combox1
Datatable dt = querysql ("select * From tb_img where id =" + combobox1.selectedvalue. tostring ());
// Convert values into byte Arrays
Byte [] mydata = (byte []) dt. Rows [0] ["IMG"];
// Write the byte [] array to the stream
Memorystream S = new memorystream ();
S. Write (mydata, 0, mydata. Length );
// The stream loaded by picturebox1
Picturebox1.image = image. fromstream (s );
}

// When the form is started, combox1 binds the database
Private void frmsaveimg_load (Object sender, eventargs E)
{
Combobox1.valuemember = "ID ";
Combobox1.displaymember = "ID ";
Combobox1.datasource = querysql ("select ID from tb_img ");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.