Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6949513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:57:52+00:00 2026-05-27T13:57:52+00:00

I’m making a program with c# and sql server and I have a problem

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:57:53+00:00Added an answer on May 27, 2026 at 1:57 pm
    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.