Upload a picture to your database

Source: Internet
Author: User
Tags oracleconnection access database connectionstrings
Let me summarize how to save pictures to SQL Server, Oracle, and Access databases.
First of all, we want to understand that the picture is stored in binary form in the database, then the steps to save the picture in the database are in general
1. Convert the picture into a binary array (byte[]);
2. Pass the converted binary array (byte[]) as a parameter to the command to be executed;
3. Execute command;

First, how to convert a picture to byte[], if you're using a asp.net2.0, you can use the FileUpload control to implement
Byte[] Filedata = this. Fileupload1.filebytes;
If you are using asp.net1.1 or you are creating WinForm then you can use the following method to convert the picture to byte[] public byte [] getBytes (String filePath)
{
System.IO.FileStream fs = new System.IO.FileStream (FilePath, System.IO.FileMode.Open);
byte[] Imgdata = new Byte[fs. Length];
Fs. Read (imgdata, 0, (int) fs. Length);
return imgdata;
What we're going to do next is to pass the resulting byte[] as a parameter to the Command object

1.SqlServer database. SQL Server has an image field type that can store up to 2G of data. byte [] Filedata = this. Fileupload1.filebytes;

String sql = "INSERT INTO T_img (IMG) VALUES (@img)";
String strconn = system.configuration.configurationmanager.connectionstrings["Fengdongdb"]. ToString ();
SqlConnection sqlconn = new SqlConnection (strconn);
SqlCommand Sqlcomm = new SqlCommand (sql, sqlconn);
SQLCOMM.PARAMETERS.ADD ("@img", sqldbtype.image); Add parameters
sqlcomm.parameters["@img"]. Value = Filedata; Assigning values to a parameter

Sqlconn.open ();
Sqlcomm.executenonquery ();
Sqlconn.close ();
2.Oracle database. We can use BLOB field types in Oracle databases to store up to 4G of data.
byte [] Filedata = this. Fileupload1.filebytes;

String sql = "INSERT into t_img (imgid,imgdata) VALUES (100,:imgdata)";
String strconn = system.configuration.configurationmanager.connectionstrings["Connectionstringfororacle"]. ToString ();
OracleConnection oraconn = new OracleConnection (strconn);
OracleCommand Oracomm = new OracleCommand (sql, oraconn);
OraComm.Parameters.Add (": Imgdata", Oracletype.blob); Add parameters
oracomm.parameters[": Imgdata"]. Value = Filedata; Assigning values to a parameter

Oraconn.open ();
Oracomm.executenonquery ();
Oraconn.close ();

Note: Here I need to explain that you should be careful when passing parameters with Oracle's dedicated connection, look at the SQL statement above and you will know that to add a ":" in front of the parameter name, the following error "oracleexception:ora-01036: Invalid variable name /number ". We need to pay attention here. There's another place, When I refer to the System.Data.OracleClient namespace, the default is not, you must add a reference to System.Data.OracleClient, I remember under VS2003 if the OracleClient is installed without adding a reference can be introduced. You should also pay attention here.

3.Access database. In Access we use OLE Object field types to support up to 1G of data. byte [] Filedata = this. Fileupload1.filebytes;

String sql = "INSERT into t_img (Imgdata) VALUES (?)";
String strconn = system.configuration.configurationmanager.connectionstrings["Connectionstringforaccess"]. ToString ();

OleDbConnection oleconn = new OleDbConnection (strconn);
OleDbCommand Olecomm = new OleDbCommand (sql, oleconn);
OLECOMM.PARAMETERS.ADD ("Imgdata", oledbtype.binary);
olecomm.parameters["Imgdata"]. Value = Filedata;

Oleconn.open ();
Olecomm.executenonquery ();
Oleconn.close ();


OK, here we'll save the pictures in the database, and then we'll talk about how to read the pictures from the database. In fact, this is the opposite of the insertion process: the first newspaper converts the image data obtained from the database to an array, and then converts the arrays to pictures. There is no significant difference between the data and I only list the data from the Oracle database for reference. Private byte [] getimagedatafromoracle ()
{
String sql = "Select Imgdata from t_img where imgid=100";
String strconn = system.configuration.configurationmanager.connectionstrings["Connectionstringfororacle"]. ToString ();
OracleConnection oraconn = new OracleConnection (strconn);
OracleCommand Oracomm = new OracleCommand (sql, oraconn);

Oraconn.open ();
Byte[] Filedata = (byte[]) oracomm.executescalar ();
Oraconn.close ();

return filedata;
}

When we get the data, the process of converting byte[to a picture is the same. Private System.Drawing.Image convertbytetoimg (Byte [] imgdata)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream (imgdata);
System.Drawing.Image 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.