本文介绍了从SQL C#检索pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Some help please Insert into sql
yte[] 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(); } } }
我尝试过的事情: 选择更改数据网格放这个代码
What I have tried: Selection changed datagrid put this code
Error is when retrieve file...(no have error) noting hapend....what I wrong? I think at line 3 to line 9
try { if (documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value != null) { byte[] ap = (byte[])documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value; MemoryStream ms = new MemoryStream(ap); axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray()); } else { axAcroPDF1.src = null; } } catch { axAcroPDF1.src = null; }
推荐答案
首先不要吞下你的异常:当你使用空白捕获,并且对它没有任何用处时,你扔掉了识别问题并解决问题所需的所有信息。 这样做: Start by not swallowing your exception: when you use a blank catch, and do nothing useful with it, you throw away all the information you need to identify the problem and solve it. Do this: try { ... } catch (Exception ex) { ... log the exception, display it in a message box, ... or stuff it on your web page - we have no idea ... what environment you are working in axAcroPDF1.src = null; }
然后,当代码运行时,您可以查看异常并希望看到出现了什么问题。但没有它,你在黑暗中工作......
Then, when the code is run, you can look at the exception and hopefully see what is going wrong. But without that, you are working in the dark...
更多推荐
从SQL C#检索pdf文件
发布评论