Friends

Rate

  

Parmaja.com

Forums for programming, Open source and free software.

You are not logged in.

  • Index
  •  » Programming
  •  » How to write & read file binary data to MS SQl Server DB

#1 2009-04-23 10:45:46

Hussein
Member
From: Syria - Damascus
Registered: 2004-05-19
Posts: 28

How to write & read file binary data to MS SQl Server DB

This topic contains algorithm explain how to write & read file stream to MS SQL-Server DataBase.

To try this code; first of all you have to create database with name: [DBFileStore], then add table [tblFileStore]. to do this you can use the foloowing SQL Statement as follow:

Code: sql

/* Create The DataBase*/
CREATE DATABASE [DBFileStore] ON  PRIMARY 
( NAME = N'DBFileStore'DBFileStore', FILENAME = N', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DBFileStore.mdf'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DBFileStore.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'DBFileStore_log'DBFileStore_log', FILENAME = N', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DBFileStore_log.ldf'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DBFileStore_log.ldf' , SIZE = 4672KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
 

/* Create Table*/
USE [DBFileStore]
CREATE TABLE [dbo].[tblFileStore]
(
  [FileName] [nvarchar](50) NOT NULL,
  [FileExtension] [nchar](10) NOT NULL,
  [FileBinaryData] [image] NOT NULL
)


Now add form with two buttons named (btnStore & btnExtract); then paste the following code in form body source code:

Code: vb

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace File_Store
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public SqlConnection sqlConnection = new SqlConnection("Server=localhost;DataBase=DBFileStore;Integrated Security=SSPI");

        private void btnStore_Click(object sender, EventArgs e)
        {
            if (opnDlg.ShowDialog() == DialogResult.OK) 
            {
                FileInfo fi = new FileInfo(opnDlg.FileName);
                string fileName = fi.Name;
                string fileExtension = fi.Extension;
                FileStream fs = new FileStream(opnDlg.FileName, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                byte[] fileData = br.ReadBytes((int)fs.Length);
                fs.Close();
                br.Close();
                
                string strSQL = "INSERT INTO tblFileStore (FileName, FileExtension, FileBinaryData)" +
                    "VALUES (@FileName, @FileExtension, @FileBinaryData)";

                SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConnection);

                sqlCmd.Parameters.Add(new SqlParameter("@FileName", SqlDbType.NVarChar, 50)).Value = fileName;
                sqlCmd.Parameters.Add(new SqlParameter("@FileExtension", SqlDbType.NChar, 10)).Value = fileExtension;
                sqlCmd.Parameters.Add(new SqlParameter("@FileBinaryData", SqlDbType.Image)).Value = fileData;

                sqlConnection.Open();
                sqlCmd.ExecuteNonQuery();
                sqlConnection.Close();

                MessageBox.Show("File Name: \n" + fi.FullName + "\nhas been saved to file store");

            }
        }

        private void btnExtract_Click(object sender, EventArgs e)
        {
            string strSQL = "SELECT TOP(1) FileName, FileExtension, FileBinaryData FROM tblFileStore";
            SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConnection);
            sqlConnection.Open();
            SqlDataReader dr = sqlCmd.ExecuteReader();
            while (dr.Read())
            {
                byte[] fileData = ((byte[])(dr["FileBinaryData"]));
                FileStream fs = new FileStream(@"c:\" + dr["FileName"], FileMode.CreateNew);
                BinaryWriter br = new BinaryWriter(fs);
                br.Write(fileData);
                fs.Close();
                br.Close();
                MessageBox.Show("File Name: \n" + dr["FileName"].ToString() + "\nhas been saved to directory C:\\");
            }
            sqlConnection.Close();            
        }
    }
}

Offline

 
  • Index
  •  » Programming
  •  » How to write & read file binary data to MS SQl Server DB

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson

[ Generated in 0.083 seconds, 7 queries executed ]