Hi,
-- First Create the database
Create Database Test_DB;
-- After execution of First command execute the following query
use Test_DB;
CREATE TABLE [dbo].[file_db](
[file_name] [varchar](50) NOT NULL,
[file_Content] [varbinary](max) NULL,
)
void insertFile()
{
FileStream fs = new FileStream(@"c:\file\input.txt", FileMode.Open, FileAccess.Read);
int length = Convert.ToInt32(fs.Length);
byte[] data = new byte[length];
fs.Read(data, 0, length);
fs.Close();
string insert = "insert into file_db values('input.txt',@file_content)";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(con);
conn.Open();
SqlCommand cmd = new SqlCommand(insert, conn);
cmd.Parameters.Add(new SqlParameter("@file_content", (object)data));
cmd.ExecuteNonQuery();
conn.Close();
}
void ReadFile()
{
SqlConnection conn = new SqlConnection(con);
string qry = "select file_content from file_db where file_name='input.txt'";
SqlCommand cmd = new SqlCommand(qry, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
byte[] output = (byte[])dr[0];
dr.Close();
conn.Close();
FileStream fs = new FileStream(@"C:\file\output.txt", FileMode.Create, FileAccess.Write);
fs.Write(output, 0, output.Length);
fs.Close();
}