Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Insert PDF in database

Is this good way?

What I have tried:

private void Button2_Click(object sender, EventArgs e)   //Save pdf in database
        {
            byte[] filedata = null;
            MemoryStream ms = new MemoryStream();
            filedata = ms.GetBuffer();
            axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());



                    using (SqlConnection openCon = new SqlConnection(cs))
                    {

                        string saveStaff = "declare @maxNo integer = 0 select @maxNo = isnull(max(number), 0) from [dbo].[documents]; Set @maxNo=@maxNo+1; INSERT into dbo.documents (number,file_location, pdf_file) VALUES (@maxNo,@file_location,@pdf_file)";

                        using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
                        {
                            querySaveStaff.Connection = openCon;

                            querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;

                            querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = filedata;

                            openCon.Open();
                            querySaveStaff.ExecuteNonQuery();
                            openCon.Close();                            


                        }

                    }

}
Posted
Updated 6-Aug-18 2:46am
Comments
Richard MacCutchan 6-Aug-18 3:04am    
Do you have a question?
Goran Bibic 6-Aug-18 3:15am    
Yes. That is question. Why in db write just 0x

When put picture have 0xFFD8FFE000104A46494600010101000000000000FFE2021C4943435F50524F........

And I can read from db file

Ok?
Patrice T 6-Aug-18 3:11am    
'Is this good way?'
Does it work ?
Goran Bibic 6-Aug-18 3:15am    
Yes. That is question. Why in db write just 0x

When put picture have 0xFFD8FFE000104A46494600010101000000000000FFE2021C4943435F50524F........

And I can read from db file

Ok?

While the actual storing of the PDF byte array is roughly correct, you have a fundamental problem in your ID generation. Rather than manually adding an ID yourself, consider changing this field to an autoincrement and let that take care of itself. As it stands, you run the risk of having two fields attempt to add the same ID in a multi-user system because there's no guarantee that you will complete the insert sequence before the next one commences.
 
Share this answer
 
What is this code supposed to do?
C#
            byte[] filedata = null;   // filedata contains nothing
            MemoryStream ms = new MemoryStream(); // a new empty memory stream
            filedata = ms.GetBuffer();  // filedata still contains nothing
            axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray()); // no idea what this is supposed to do

// ...

querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = filedata;
// but you never add any content to filedata, so it adds 0x0 to the database.
 
Share this answer
 
Comments
Goran Bibic 6-Aug-18 13:15pm    
Some help?
Richard MacCutchan 6-Aug-18 13:29pm    
Some help: you just need to read the contents of the file into the filedata array. A simple operation using a streamreader.
Goran Bibic 6-Aug-18 13:35pm    
I solve that and work...just need help to retrieve from db.

Save like this

private void Button2_Click(object sender, EventArgs e) //Save //snimi
{
OpenFileDialog ofd = new OpenFileDialog() { Filter = "PDF|*.pdf" };//Open pdf file
if (ofd.ShowDialog() == DialogResult.OK)
{

FileStream fs = File.OpenRead(ofd.FileName);
MemoryStream ms = new MemoryStream();
axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());
fs.CopyTo(ms);

if (count_numberTextBox.Enabled == true)
{
if (string.IsNullOrEmpty(idTextBox.Text))
{

using (SqlConnection openCon = new SqlConnection(cs))
{

string saveStaff = "declare @maxNo integer = 0 select @maxNo = isnull(max(number), 0) from [dbo].[documents]; Set @maxNo=@maxNo+1; INSERT into dbo.documents (number, count_number, label, partner, tax_number, date, file_location, pdf_file) VALUES (@maxNo,@count_number,@label,@partner,@tax_number,@date,@file_location,@pdf_file)";

using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
{
querySaveStaff.Connection = openCon;
querySaveStaff.Parameters.Add("@count_number", SqlDbType.VarChar, 255).Value = count_numberTextBox.Text;
querySaveStaff.Parameters.Add("@label", SqlDbType.VarChar, 255).Value = labelTextBox.Text;
querySaveStaff.Parameters.Add("@partner", SqlDbType.VarChar, 255).Value = partnerComboBox.Text;
querySaveStaff.Parameters.Add("@tax_number", SqlDbType.VarChar, 255).Value = tax_numberTextBox.Text;
querySaveStaff.Parameters.Add("@date", SqlDbType.Date).Value = dateDateTimePicker.Text;
querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;
querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = ms.ToArray();

openCon.Open();
querySaveStaff.ExecuteNonQuery();
openCon.Close();

dateDateTimePicker.Enabled = false;
count_numberTextBox.Enabled = false;
file_locationTextBox.Enabled = false;
labelTextBox.Enabled = false;
partnerComboBox.Enabled = false;
tax_numberTextBox.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;


}

}
}
else
{

using (SqlConnection openCon = new SqlConnection(cs))
{
string saveStaff = "UPDATE dbo.documents SET count_number=@count_number, label=@label, partner=@partner, tax_number=@tax_number, date=@date, file_location=@file_location, pdf_file=@pdf_file WHERE id= " + idTextBox.Text;

using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
{

querySaveStaff.Connection = openCon;
querySaveStaff.Parameters.Add("@count_number", SqlDbType.VarChar, 255).Value = count_numberTextBox.Text;
querySaveStaff.Parameters.Add("@label", SqlDbType.VarChar, 255).Value = labelTextBox.Text;
querySaveStaff.Parameters.Add("@partner", SqlDbType.VarChar, 255).Value = partnerComboBox.Text;
querySaveStaff.Parameters.Add("@tax_number", SqlDbType.VarChar, 255).Value = tax_numberTextBox.Text
Richard MacCutchan 7-Aug-18 2:48am    

FileStream fs = File.OpenRead(ofd.FileName);
MemoryStream ms = new MemoryStream();
axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());
fs.CopyTo(ms);

I still do not understand what all the above code is about. You only need to read the file into a byte array in order to get it into the database. Then to retrieve it you read the database record into another byte array and write that to a new file. Converting to strings and memory streams is just a waste of time and effort.
Richard MacCutchan 6-Aug-18 13:34pm    
I notice also you have a field called file_location. What is that supposed to be for? If you are saving the location of the file on disk and the content, then one of those fields is redundant.

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