Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload / Download file from SQL Server 2005/2008 from WinForms C# app?

I'm using DocX to create .docx files. Instead of storing them on hard drive, Share or SharePoint i would prefer to store them inside SQL Database. So my questions are:

  1. How to write proper code to save file to that database?
  2. How to write proper code to retrive file to that database?
  3. What kind of datatype should i set for a table to hold the docx files? Is it Image?

With regards,

MadBoy

PS. I would prefer a proper code in terms of using Using the old 'school' way:

using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetailsDepozyt))
using (var sqlQuery = new SqlCommand(preparedCommand, varConnection))
using (var sqlQueryResult = sqlQuery.ExecuteReader())
    while (sqlQueryResult != null && sqlQueryResult.Read())
like image 356
MadBoy Avatar asked Feb 13 '10 21:02

MadBoy


People also ask

How do I upload a file to SQL Server?

In Object Explorer, connect to an instance of the SQL Server Database Engine and then expand that instance. Expand Databases, right-click the database from which to add the files, and then click Properties. In the Database Properties dialog box, select the Files page. To add a data or transaction log file, click Add.

How do I upload files to Winforms?

Start(fileName) to 'View Files'. This method opens the file through its default process. You can add the fileName to the list after it has been uploaded and include the Process. Start method in the View button Click event handler.


1 Answers

A docx file is like a zip file which is collection of several files. What about converting into binary and then saving into DB

Something as followswill work

To save

        string filePath = "";
        string connectionString = "";
        FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        BinaryReader reader = new BinaryReader(stream);
        byte[] file = reader.ReadBytes((int)stream.Length);
        reader.Close();
        stream.Close();

        SqlCommand command;
        SqlConnection connection = new SqlConnection(connectionString);
        command = new SqlCommand("INSERT INTO FileTable (File) Values(@File)", connection);
        command.Parameters.Add("@File", SqlDbType.Binary, file.Length).Value = file;
        connection.Open();
        command.ExecuteNonQuery();

Retrieval is a bit complicated process as follows

            SqlConnection connection = new SqlConnection("");
            string query = 
            @" SELECT File FROM FileTable where FileID =" + 125;
            SqlCommand command = new SqlCommand(query, connection);

            FileStream stream;
            BinaryWriter writer;

            int bufferSize = 100;
            byte[] outByte = new byte[bufferSize];

            long retval;
            long startIndex = 0;

            string pubID = "";

            connection.Open();
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.Default);

            while (reader.Read())
            {    
                pubID = reader.GetString(0);

                stream = 
                new FileStream("abc.docx", FileMode.OpenOrCreate, FileAccess.Write);
                writer = new BinaryWriter(stream);
                startIndex = 0;
                retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);

                while (retval == bufferSize)
                {
                    writer.Write(outByte);
                    writer.Flush();
                    startIndex += bufferSize;
                    retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
                }
                writer.Write(outByte, 0, (int)retval - 1);
                writer.Flush();
                writer.Close();
                stream.Close();
            } reader.Close();
            connection.Close();

Have look at following articles for help and details

Saving:

  • http://msdn.microsoft.com/en-us/library/4f5s1we0(VS.80).aspx

Retreival :

  • http://msdn.microsoft.com/en-us/library/87z0hy49(VS.80).aspx
like image 66
Asad Avatar answered Nov 07 '22 01:11

Asad