Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Reading and Writing BLOB Data to Microsoft SQL or Oracle Database

4.77/5 (17 votes)
22 Dec 2009CPOL 173K  
In this article, I will examine how to store and retrieve binary files such as image or PDF into Microsoft SQL or Oracle database.

Introduction

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

Using the Code

Reading a File into a Byte Array

C#
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.

SQL
CREATE TABLE BlobStore 
( 
    ID number, 
    BLOBFILE BLOB, 
    DESCRIPTION varchar2(100) 
);

Now, we would like to write Blob in Oracle using C#.

C#
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 the next step, we would like to load data from Oracle to file.

C#
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 Microsoft 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.

SQL
CREATE TABLE TestTable 
( 
    ID int, 
    BlobData varbinary(max), 
    DESCRIPTION nvarchar(100) 
)

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

C#
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.

C#
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 Microsoft SQL database.

History

  • 22nd December, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)