ASP tutorial How to save to a database tutorial for. NET upload files (C #)
C # do a small program, it involves the access and display of photos, search the internet for a lot of relevant code, but almost no complete, most of which is only the access or display code, the author will be sorted out, forming a set of photos uploaded to the database, photos displayed in one of the small modules. Using
System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.IO;
Using System.Data.SqlClient;
Namespace WindowsApplication1
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
Select button Event Handling
private void Button1_Click (object sender, EventArgs e)
{
Openfiledialog1.initialdirectory = "C:";
Openfiledialog1.filter = "Picture file (*.jpg) |*.jpg";
Openfiledialog1.filterindex = 1;
Openfiledialog1.restoredirectory = true;
Openfiledialog1.multiselect = true;
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
TextBox1.Text = Openfiledialog1.filename;
}
}
Upload button Event handling
private void Button2_Click (object sender, EventArgs e)
{
Save (Phototoarray (textBox1.Text.ToString ()));
}
Convert picture information to binary information
Private byte[] Phototoarray (string path)
{
FileStream stream = new FileStream (path, FileMode.Open, FileAccess.Read);
byte[] Bufferphoto = new Byte[stream. Length];
Stream. Read (Bufferphoto, 0, Convert.ToInt32 (stream). Length));
Stream. Flush ();
Stream. Close ();
return Bufferphoto;
}
Inserting binary pictures into the database
private void Save (byte[] image)
{
String sql = "INSERT into Photo (photo_info) VALUES (@photo)";
SqlParameter param = new SqlParameter ();
param = new SqlParameter ("@photo", sqldbtype.image);
Param. Value = image;
SqlCommand COMMD = new SqlCommand (sql, Dbhelper.con);
Commd. Parameters.Add (param);
Try
{
DBhelper.con.Open ();
Commd. ExecuteNonQuery ();
MessageBox.Show ("You have successfully inserted the picture into the database!") ");
}
catch (Exception ex)
{
MessageBox.Show (ex. message);
}
Finally
{
DBhelper.con.Close ();
}
}
Show Picture button event handling
private void Button3_Click (object sender, EventArgs e)
{
String strSQL = "SELECT [Photo_info] from [photo] Where [photo_id]= (@photo)";
SqlParameter param = new SqlParameter ();
param = new SqlParameter ("@photo", SqlDbType.Int);
Param. Value = ComboBox1.Text.ToString ();
SqlCommand cmd = new SqlCommand (strSQL, Dbhelper.con);
Cmd. Parameters.Add (param);
DBhelper.con.Open ();
System.Data.SqlClient.SqlDataReader reader = cmd. ExecuteReader ();
Try
{
Reader. Read ();
MemoryStream ms = new MemoryStream ((byte[)) reader["Photo_info");
System.Drawing.Image Image = System.Drawing.Image.FromStream (MS, true);
This.pictureBox1.Image = Image;
}
catch (Exception ex)
{
MessageBox.Show (ex. message);
}
Finally
{
DBhelper.con.Close ();
}
}
private void Form1_Load (object sender, EventArgs e)
{
TODO: This line of code loads the data into the table "Myphotodataset.photo". You can move or remove it as needed.
This.photoTableAdapter.Fill (This.myPhotoDataSet.Photo);
}
}
}