Reading and Writing BLOB data to ms SQL or Oracle Database

Source: Internet
Author: User
Tags oracleconnection connectionstrings
Introduction

In this article, I will examine how to store and retrieve binary files such as image or PDF into ms SQL or Oracle database.

Using the code

Reading a file into a byte array.

Collapse copy code
        byte[] byteArray = null;using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read)){   byteArray = new byte[fs.Length];   int iBytesRead = fs.Read(byteArray, 0, (int)fs.Length);}         

Saving BLOB data from a file to Oracle.

For Oracle, you will have to download ODP. Net from Oracle. The following script will create a table that will hold the BLOB data in Oracle.

Collapse copy code
        CREATE TABLE BlobStore (     ID number,     BLOBFILE BLOB,     DESCRIPTION varchar2(100) );         

Now, we woshould like to write blob in Oracle using C #.

Collapse copy code
        string sql = " INSERT INTO BlobStore(ID,BLOBFILE,DESCRIPTION) VALUES(:ID, :BLOBFILE, :DESCRIPTION) "; string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (OracleConnection conn = new OracleConnection(strconn)) {     conn.Open();      using (OracleCommand cmd = new OracleCommand(sql, conn))     {         cmd.Parameters.Add("ID", OracleDbType.Int32, 1, ParameterDirection.Input);         cmd.Parameters.Add("BLOBFILE", OracleDbType.Blob, byteArray , ParameterDirection.Input);         cmd.Parameters.Add("DESCRIPTION", OracleDbType.Varchar2, "any thing here", ParameterDirection.Input);         cmd.ExecuteNonQuery();     } }         

In next step, we wowould like to load a data from Oracle to file.

Collapse copy code
        string sql = " select * from BlobStore "; string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (OracleConnection conn = new OracleConnection(strconn)) {   conn.Open();   using (OracleCommand cmd = new OracleCommand(sql, conn))   {       using (IDataReader dataReader = cmd.ExecuteReader())       {           while (dataReader.Read())           {              byte[] byteArray= (Byte[])dataReader["BLOBFILE"];              using (FileStream fs = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write))              {                 fs.Write(byteArray, 0, byteArray.Length);              }           }       }    } }         

Saving BLOB data from a file to MS SQL

Storing and retrieving BLOB data in SQL Server is similar to Oracle. here are code snippets that show saving and loading in SQL Server. the following script will create a table that will hold the BLOB data in SQL Server.

Collapse copy code
        CREATE TABLE TestTable (     ID int,     BlobData varbinary(max),     DESCRIPTION nvarchar(100) )         

The following code shows how to load from SQL Server to file.

Collapse copy code
        using (SqlConnection connection = new SqlConnection("ConnectionString"))      {                connection.Open();                using (SqlCommand command = new SqlCommand("select BlobData from TestTable", connection))                {                       byte[] buffer = (byte[])command.ExecuteScalar();                        using (FileStream fs = new FileStream(@"C:/test.pdf", FileMode.Create))                        {                            fs.Write(buffer, 0, buffer.Length);                        }                 }      }        

The following code shows how to save from byte array to SQL Server.

Collapse copy code
        using (SqlConnection connection = new SqlConnection("ConnectionString")) {     connection.Open();     using(SqlCommand cmd = new SqlCommand("INSERT INTO TestTable(ID, BlobData, DESCRIPTION) VALUES (@ID, @BlobData, @DESCRIPTION)", conn))     {         cmd.Parameters.Add("@ID", SqlDbType.int).Value = 1;         cmd.Parameters.Add("@BlobData", SqlDbType.VarBinary).Value = ByteArray;         cmd.Parameters.Add("@DESCRIPTION", SqlDbType.NVarchar).Value = "Any text Description";         cmd.ExecuteNonQuery();             } }   
Summary

In this article, we examined how to store and retrieve binary files such as image or PDF Into ORACLE or ms SQL database

Related Article

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.