Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to save PDF into MySQL via C# Winform and retrieve into gridview. Preview open into adobe reader of selected row of gridview.

What I have tried:

C#
if (openFileDialog.ShowDialog() == DialogResult.OK)
{label1.Text = openFileDialog.FileName;}
string[] FullFileName = openFileDialog.FileName.Split('\\');
string fname = FullFileName.Last().ToString();
byte[] FileContent;
FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open);
BinaryReader bs = new BinaryReader(fs);
FileContent = bs.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
bs.Close();
string Query = "insert into D (FileName,[File]) values(@name,@data)";
MySqlCommand cmd = new MySqlCommand(Query, con);
con.Open();
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@name", fname);
cmd.Parameters.AddWithValue("@data", FileContent);
cmd.ExecuteNonQuery();
MessageBox.Show("data saved");

my error are:
MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[File]) values('Receipt.pdf',_binary '%PDF-1.3
%âãÏÃ
Posted
Updated 1-Sep-19 23:02pm
v2
Comments
Richard MacCutchan 2-Sep-19 4:16am    
Remove the square brackets from the column name "File".
Member 14192879 2-Sep-19 5:14am    
what datatype I have to use for 'file' column?
Richard MacCutchan 2-Sep-19 5:18am    
Look at the MySQL documentation for the one that is best suited to your data content: MySQL :: MySQL 8.0 Reference Manual :: 11.4.3 The BLOB and TEXT Types[^]
Member 14192879 2-Sep-19 5:33am    
can you tell me how can retrieve upload pdf into GridView?

1 solution

MySql doesn't use square brackets round ambiguous or "spaced" column names, it uses backquotes instead:
SQL
INSERT INTO D (FileName, `File`) Values (...
 
Share this answer
 
Comments
Member 14192879 2-Sep-19 5:34am    
HOW CAN I RETRIEVE PREVIOUS PDF STORED FILE?
OriginalGriff 2-Sep-19 5:42am    
Don't SHOUT.
Member 14192879 2-Sep-19 5:37am    
DataTable dt = new DataTable();
MySqlDataReader myReader = null;
MySqlCommand cmd = new MySqlCommand("SELECT `FileName` FROM `D` WHERE `clientid` LIKE '%" + textBox31.Text + "%'", con);
con.Open();
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
//label115.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
label115.Text = (myReader["FileName"].ToString());
}
con.Close();
myReader.Close();



private void label115_Click(object sender, EventArgs e)
{
axAcroPDF1.src = label115.Text;
}



IS THIS CORRECT CODE?

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