Sql server 如何从数据库中提取.pdf文件,然后将其作为附件插入电子邮件,而不将其写入文件结构

Sql server 如何从数据库中提取.pdf文件,然后将其作为附件插入电子邮件,而不将其写入文件结构,sql-server,vb.net,pdf,Sql Server,Vb.net,Pdf,我正在迁移web服务器并重写一些应用程序(vb.net环境)。在此过程中,我遇到了一个难题,非常感谢您的帮助 我可以成功地从数据库中拉出.pdf文件并显示它: Dim iID As Integer Dim bPDF As Byte() iID = Convert.ToInt32(Request.QueryString("Id")) bPDF = CodeMod.GetPdfBytes(iID) Response.ContentType = "application/pdf" Response.O

我正在迁移web服务器并重写一些应用程序(vb.net环境)。在此过程中,我遇到了一个难题,非常感谢您的帮助

我可以成功地从数据库中拉出.pdf文件并显示它:

Dim iID As Integer
Dim bPDF As Byte()
iID = Convert.ToInt32(Request.QueryString("Id"))
bPDF = CodeMod.GetPdfBytes(iID)
Response.ContentType = "application/pdf"
Response.OutputStream.Write(bPDF, 0, bPDF.Length)

---------------------------------

Public Function GetPdfBytes(ByVal id As Int32) As Byte()

Dim SQLcon As New SqlClient.SqlConnection
Dim SQLcmd As New SqlClient.SqlCommand
Dim dt As New DataTable

SQLcon.ConnectionString = sConnection("pdfStore")
SQLcmd.CommandText = "[DOIMSQL,1433].[DOI_PDF_Storage].dbo.sel_pdf_id"
SQLcmd.CommandType = CommandType.StoredProcedure
SQLcmd.Parameters.Add("@pdf_id", SqlDbType.VarChar).Value = id
SQLcmd.Connection = SQLcon

SQLcon.Open()
dt.Load(SQLcmd.ExecuteReader)
SQLcon.Close()

GetPdfBytes = DirectCast(dt.Rows(0)("pdf_file"), Byte())

End Function
我可以成功地发送带有文件附件的电子邮件:

CodeMod.sendMail(strTo, strFrom, "Test Email", "Test Content",, Server.MapPath("/Test/TEST.pdf"))

---------------------------------
Public Sub sendMail(strTo As String, strFrom As String, strSubject As String, strMsg As String, Optional ByVal strCc As String = "", Optional ByVal file As String = "")

Dim oMMsg As New Net.Mail.MailMessage()
oMMsg.From = New MailAddress(strFrom)
oMMsg.To.Add(strTo)
oMMsg.Subject = strSubject
oMMsg.Body = strMsg
oMMsg.IsBodyHtml = True

If file <> "" Then oMMsg.Attachments.Add(New Attachment(file))

Dim client As New SmtpClient()
client.Host = "correct.information"
client.Port = 25
client.Send(oMMsg)

End Sub
CodeMod.sendMail(strTo,strFrom,“测试电子邮件”,“测试内容”,Server.MapPath(“/Test/Test.pdf”))
---------------------------------
Public Sub sendMail(strTo作为字符串,strFrom作为字符串,strSubject作为字符串,strMsg作为字符串,可选ByVal strc作为字符串=”,可选ByVal file作为字符串=”)
Dim oMMsg作为新的Net.Mail.MailMessage()文件
oMMsg.From=新邮件地址(strFrom)
oMMsg.To.Add(strTo)
oMMsg.Subject=strSubject
oMMsg.Body=strMsg
oMMsg.IsBodyHtml=True
如果文件为“”,则为oMMsg.Attachments.Add(新附件(文件))
作为新SmtpClient()的Dim客户端
client.Host=“correct.information”
client.Port=25
客户端发送(oMMsg)
端接头
我需要做的是结合这两种能力。我需要从数据库中检索.pdf并将其作为电子邮件附件发送


…无需将文件写入文件结构。

如果我没有弄错,您可以将该PDF写入内存流,并直接从内存流制作附件。关于如何从内存流附加,存在一个单独的问题


请跟随,但不要忘记将流“倒带”到位置0。花了一点时间将其从C#转换到VB.Net,但最终这为我指明了正确的方向。谢谢,谢尔盖。