Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open word document (saved as binary) from database

I'm making a program with c# and sql server and I have a problem , I hope if anyone help me .

I will but the database on pc and the program will be installed in other PCs , and app pcs' program connected to that database.

the program saving documents (word -excel) as binary ,using this code:

  byte[] ReadFile(string sPath)
    {
        //Initialize byte array with a null value initially.
        byte[] data = null;

        //Use FileInfo object to get file size.
        FileInfo fInfo = new FileInfo(sPath);
        long numBytes = fInfo.Length;

        //Open FileStream to read file
        FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

        //Use BinaryReader to read file stream into byte array.
        BinaryReader br = new BinaryReader(fStream);

        //When you use BinaryReader, you need to supply number of bytes to read from file.
        //In this case we want to read entire file. So supplying total number of bytes.
        data = br.ReadBytes((int)numBytes);
        return data;
    }

 private void button1_Click(object sender, EventArgs e)
    {
        string dt = dateTimePicker1.Value.ToShortDateString();

        byte[] red = ReadFile(textBox3.Text);
        con.Open();
        string qry = "insert into documents ([Account no],Name,[Phone number],Date,[Document name],Document,Type) values(@accon,@name,@phone,@date,@docname,@doc,@type)";

        //Initialize SqlCommand object for insert.
        SqlCommand SqlCom = new SqlCommand(qry, con);

        //We are passing Original Image Path and Image byte data as sql parameters.

        SqlCom.Parameters.Add(new SqlParameter("@accon", textBox1.Text));
        SqlCom.Parameters.Add(new SqlParameter("@name", textBox2.Text));
        SqlCom.Parameters.Add(new SqlParameter("@phone", textBox3.Text));
        SqlCom.Parameters.Add(new SqlParameter("@date", dt));
        SqlCom.Parameters.Add(new SqlParameter("@docname", textBox1.Text));
         SqlCom.Parameters.Add(new SqlParameter("@doc", (object)red));

        SqlCom.Parameters.Add(new SqlParameter("@type", (object)textBox2.Text));
        SqlCom.ExecuteNonQuery();
        con.Close();

        MessageBox.Show("done");
    }

the problem : that I don't know how to retrieve saved documents in database and open it with Microsoft word or Microsoft Excel according to their types.

I want to select specific document form database and open it

Thanks in advance

like image 722
moonshine Avatar asked Dec 16 '11 08:12

moonshine


People also ask

Are Word files binary files?

The Microsoft Word Binary File format, with the . doc extension and referred to here as DOC, was the default format used for documents in Microsoft Word from Word 97 (released in 1997) through Microsoft Office 2003.

Can MS Word working with database?

Microsoft Word has a Mail Merge feature that links a Word document with information stored in a data file, called a data source. The data source can be a database. Before the merge, you can create and manage a database within Word. The database is saved as an Access database file within Word.

How do I open a data file in Word?

Within Word, access the File menu and select Open to locate your DAT file on the hard drive or linked storage device. Select the file to open it within Word. With the file now visible in the Word document, return to the File menu and select Save As. Choose the new file type desired to convert the document.


Video Answer


1 Answers

String connStr = "connection string";

// add here extension that depends on your file type
string fileName = Path.GetTempFileName() + ".doc";

using (SqlConnection conn = new SqlConnection(connStr))
{
    conn.Open();
    using (SqlCommand cmd = conn.CreateCommand())
    {
        // you have to distinguish here which document, I assume that there is an `id` column
        cmd.CommandText = "select document from documents where id = @id";
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = 1;
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                int size = 1024 * 1024;
                byte[] buffer = new byte[size];
                int readBytes = 0;
                int index = 0;

                using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    while ((readBytes = (int)dr.GetBytes(0, index, buffer, 0, size)) > 0)
                    {
                        fs.Write(buffer, 0, readBytes);
                        index += readBytes;
                    }
                }
            }
        }
    }
}

// open your file, the proper application will be executed because of proper file extension
Process prc = new Process();
prc.StartInfo.FileName = fileName;
prc.Start();
like image 121
Michał Powaga Avatar answered Sep 20 '22 10:09

Michał Powaga