Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi
I need to create a simple database to store files that are generated through stream readers in an application. so basically once the file has been created it has to be inserted into a table . this table already exists and therefore can only be altered to allow file streaming (which has already been done) . All I need to create a file group and add a table that will store : AutoID - FileName - file(which will be the actual file) - dateTime.

then a i need to read the file again in a C# app and compile it in a string and be able to view the file exactly as it was.

1.Alter database for filegroup (we can call the databse TESTDB) SQL
2. Create table for storing files SQl
3. create procedure to insert files SQL
4. code to read file content and store it as string C#

any examples will help. thank you.
Posted
Updated 21-Nov-12 21:05pm
v2
Comments
OriginalGriff 22-Nov-12 3:07am    
And what is the problem?
What have you done? Where are you stuck?
Member 9374423 22-Nov-12 3:09am    
I am stuck on starting it up.
I have no idea how this works as this is the first time using it.
I have read through numerous websites, but I still don't understand.
OriginalGriff 22-Nov-12 3:16am    
So, what have you done on the first bullet point?
Have you prepped your DB?
Member 9374423 22-Nov-12 4:22am    
yes, I wont need file streaming,just have to get the file into the database and out again.

1 solution


Hi,
try this solution,


SQL
-- 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,
    )

C#
// It will help you to insert the file into the db
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();
        }
// It will help you to read the inserted file content from db
 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();
        }


Regards,
Sowraaj

 
Share this answer
 
Comments
Member 9374423 22-Nov-12 8:07am    
Great! thank you very much!
codeninja-C# 22-Nov-12 9:14am    
You are welcome

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900