从SQL C#检索pdf文件

编程入门 行业动态 更新时间:2024-10-20 16:49:31
本文介绍了从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文件

本文发布于:2023-10-27 03:16:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532149.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   SQL   pdf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!