Reading and Writing BLOB Data to MS SQL or Oracle Database

來源:互聯網
上載者:User
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 would 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 would 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.