从数据库中打开word文档(保存为二进制文件)

moo*_*ine 7 c# sql-server ms-word

我正在用c#和sql server制作一个程序,我有一个问题,我希望有人帮助我.

我会,但pc上的数据库和程序将安装在其他PC上,app pcs的程序连接到该数据库.

程序使用以下代码将文档(word -excel)保存为二进制文件:

  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");
    }
Run Code Online (Sandbox Code Playgroud)

问题:我不知道如何在数据库中检索已保存的文档,并根据其类型使用Microsoft word或Microsoft Excel打开它.

我想选择特定的文档表单数据库并打开它

提前致谢

Mic*_*aga 12

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();
Run Code Online (Sandbox Code Playgroud)

  • 真的我不知道该怎么感谢你,我花了很多时间在这个问题上,我没有找到合适的解决方案.非常感谢 :)))) (2认同)