VB.Net代码以将MS Word文档插入并检索到MS SQL Server 2008中 [英] VB.Net Code to Insert and retrive MS Word doc into MS SQL Server 2008
问题描述
大家好,
我在VB.Net编码方面有中级知识.我已经在网上搜索了有关
的帮助
我的问题,但无济于事.我的意思是尽管有
,但我找不到一个有帮助的人
一些基于Web的应用程序.
我有一个紧急问题,我想将一些简历存储到MS SQL Server 2008 DB中.我只想创建一个Windows窗体应用程序,该应用程序将具有一些按钮,可将简历上传到数据库中并保护它们的安全,并且当从组合框中选择该人的姓名时,将从数据库中检索特定的简历,这将约束所有简历的人名在数据库中.
Senario:
MS SQL Server 2008数据库表将具有2个字段:NameOfResumeOwner,Resume
Windows窗体:将具有2个文本框和2个按钮:
-2个文本框,用于保存ResumeOwner的名称和要上传的简历
-2按钮; 1将数据上载/插入到数据库中,另一个用于检索简历以在MS Word中进行编辑.
我下面的用于将MS Word doc插入数据库的代码可以通过插入图像数据来正常工作,但是当我想以MS Word doc的形式检索数据时,则无法正常工作.给出错误为访问被拒绝".我已获得必要的权限,以获取要保存在指定位置的MS Word文档,但无济于事.请在可能的情况下提供帮助. (注意:这是Windows窗体应用程序,而不是ASP.Net中的Web窗体应用程序,因此请不要将我指向ASP.Net站点.)
Hi All,
I have intermediate knowledge in VB.Net coding. I have searched the web to find help with
my issue but to no avail. I mean I can''t find one that is helpful though there are
some in Web based applications.
I have an urgent issue where I want to store some resumes into a MS SQL Server 2008 DB. I just want to create a windows form application which will have some buttons to upload the resumes into the DB and safe them, and retrive the specific resume from the DB when the name of the person is selected from a combobox, which will contan all the names of the people who''s resumes are in the DB.
Senario:
MS SQL Server 2008 DB table will have 2 fields: NameOfResumeOwner, Resume
Windows Form: Will have 2 textboxes and 2 Buttons:
-2 textboxes to hold the ResumeOwner''s Name and the Resume to be uploaded
-2 Buttons; 1 to upload/insert data into the DB and the other to Retrive the Resume for editing in MS Word.
my following code for the insertion of the MS Word doc into the DB works fine by inserting the image data but when I want to retrive the data as MS Word doc its not working. Giving the error as Access denied. I have given the necessary permissions to get the MS Word doc to be saved in the location as specified but to no avail. Please assist where possible. (Note: This is Windows Form application and not Web Form Applications as in ASP.Net, so don''t point me to ASP.Net sites.)
Imports System.IO
Imports System.Data.SqlClient
''Imports System.Security.Permissions.FileIOPermission
Imports System.Security.Permissions
Imports System.Security
Public Class ImageDemo
Inherits System.Windows.Forms.Form
''Inherits CodeAccessPermission
''''Implements IUnrestrictedPermission
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
''This call is required by the Windows Form Designer.
InitializeComponent()
''Add any initialization after the InitializeComponent() call
End Sub
''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
''NOTE: The following procedure is required by the Windows Form Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents btnDownload As System.Windows.Forms.Button
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents btnUpload As System.Windows.Forms.Button
Friend WithEvents txtImg As System.Windows.Forms.TextBox
Friend WithEvents lblImg As System.Windows.Forms.Label
Friend WithEvents lblImage As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents btnMSWOrd As System.Windows.Forms.Button
Friend WithEvents btnseltfile As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Panel
Me.btnMSWOrd = New System.Windows.Forms.Button
Me.btnseltfile = New System.Windows.Forms.Button
Me.btnDownload = New System.Windows.Forms.Button
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.btnUpload = New System.Windows.Forms.Button
Me.txtImg = New System.Windows.Forms.TextBox
Me.lblImg = New System.Windows.Forms.Label
Me.lblImage = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.Panel1.SuspendLayout()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
''
''Panel1
''
Me.Panel1.Controls.Add(Me.btnMSWOrd)
Me.Panel1.Controls.Add(Me.btnseltfile)
Me.Panel1.Controls.Add(Me.btnDownload)
Me.Panel1.Controls.Add(Me.PictureBox1)
Me.Panel1.Controls.Add(Me.btnUpload)
Me.Panel1.Controls.Add(Me.txtImg)
Me.Panel1.Controls.Add(Me.lblImg)
Me.Panel1.Controls.Add(Me.lblImage)
Me.Panel1.Location = New System.Drawing.Point(9, 42)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(352, 184)
Me.Panel1.TabIndex = 7
''
''btnMSWOrd
''
Me.btnMSWOrd.Location = New System.Drawing.Point(246, 135)
Me.btnMSWOrd.Name = "btnMSWOrd"
Me.btnMSWOrd.Size = New System.Drawing.Size(75, 23)
Me.btnMSWOrd.TabIndex = 6
Me.btnMSWOrd.Text = "MS WORD"
Me.btnMSWOrd.UseVisualStyleBackColor = True
''
''btnseltfile
''
Me.btnseltfile.Location = New System.Drawing.Point(304, 16)
Me.btnseltfile.Name = "btnseltfile"
Me.btnseltfile.Size = New System.Drawing.Size(32, 23)
Me.btnseltfile.TabIndex = 5
Me.btnseltfile.Text = "..."
''
''btnDownload
''
Me.btnDownload.Font = New System.Drawing.Font("Bookman Old Style", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDownload.Location = New System.Drawing.Point(248, 80)
Me.btnDownload.Name = "btnDownload"
Me.btnDownload.Size = New System.Drawing.Size(75, 23)
Me.btnDownload.TabIndex = 4
Me.btnDownload.Text = "Download"
''
''PictureBox1
''
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.PictureBox1.Location = New System.Drawing.Point(64, 48)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(176, 120)
Me.PictureBox1.TabIndex = 3
Me.PictureBox1.TabStop = False
''
''btnUpload
''
Me.btnUpload.Font = New System.Drawing.Font("Bookman Old Style", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnUpload.Location = New System.Drawing.Point(248, 48)
Me.btnUpload.Name = "btnUpload"
Me.btnUpload.Size = New System.Drawing.Size(75, 23)
Me.btnUpload.TabIndex = 1
Me.btnUpload.Text = "Upload"
''
''txtImg
''
Me.txtImg.Location = New System.Drawing.Point(112, 16)
Me.txtImg.Name = "txtImg"
Me.txtImg.Size = New System.Drawing.Size(192, 20)
Me.txtImg.TabIndex = 0
''
''lblImg
''
Me.lblImg.AutoSize = True
Me.lblImg.Font = New System.Drawing.Font("Bookman Old Style", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblImg.Location = New System.Drawing.Point(16, 16)
Me.lblImg.Name = "lblImg"
Me.lblImg.Size = New System.Drawing.Size(78, 14)
Me.lblImg.TabIndex = 2
Me.lblImg.Text = "Image Path"
Me.lblImg.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
''
''lblImage
''
Me.lblImage.AutoSize = True
Me.lblImage.Font = New System.Drawing.Font("Bookman Old Style", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblImage.Location = New System.Drawing.Point(16, 48)
Me.lblImage.Name = "lblImage"
Me.lblImage.Size = New System.Drawing.Size(45, 14)
Me.lblImage.TabIndex = 4
Me.lblImage.Text = "Image"
Me.lblImage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
''
''Label1
''
Me.Label1.AutoSize = True
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(89, 10)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(224, 17)
Me.Label1.TabIndex = 8
Me.Label1.Text = "Upload & DownLoad of images"
''
''SaveFileDialog1
''
Me.SaveFileDialog1.FileName = "doc1"
''
''ImageDemo
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(368, 238)
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.Label1)
Me.Name = "ImageDemo"
Me.Text = "Form1"
Me.Panel1.ResumeLayout(False)
Me.Panel1.PerformLayout()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Try
If Trim(txtImg.Text) = "" Then
MsgBox("Please select a image.")
Exit Sub
End If
Dim fs As New FileStream(Trim(txtImg.Text), FileMode.Open)
Dim Data() As Byte = New [Byte](fs.Length) {}
fs.Read(Data, 0, fs.Length)
'' <add name="SCARMAK.ConnString" connectionString="Data Source=WAIITD138;Initial Catalog=SCARMAK;User ID=SCARMAKUSER;Password=password"
Dim con As New System.Data.SqlClient.SqlConnection("data source=WAIITD138;Initial Catalog=WORDLOAD;Persist Security Info=True;User ID=WORDUSER;Password=password")
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("UploadImage")
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@imgsamgetimage", Data)
cmd.ExecuteNonQuery()
con.Close()
fs.Close()
Catch ex As System.Data.SqlClient.SqlException
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnseltfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnseltfile.Click
OpenFileDialog1.ShowDialog()
txtImg.Text = OpenFileDialog1.FileName
End Sub
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
''Dim con As New System.Data.SqlClient.SqlConnection("data source=WAIITD138;Initial Catalog=WORDLOAD;Persist Security Info=True;User ID=WORDUSER;Password=password")
''con.Open()
''Dim cmd As New System.Data.SqlClient.SqlCommand("select * from SampleImageTable")
''cmd.Connection = con
''cmd.CommandType = CommandType.Text
''Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
''Dim ds As New DataSet()
''da.Fill(ds)
''Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
''Dim memorybits As New MemoryStream(bits)
''Dim bitmap As New Bitmap(memorybits)
''PictureBox1.Image = bitmap
Try
Dim PictureCol As Integer = 0 '' the column # of the BLOB field
''Dim cn As New SqlConnection("server=localhost;integrated security=yes;database=NorthWind")
Dim cn As New System.Data.SqlClient.SqlConnection("data source=WAIITD138;Initial Catalog=WORDLOAD;Persist Security Info=True;User ID=WORDUSER;Password=password")
Dim cmd As New SqlCommand("Select * from SampleImageTable", cn)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New FileStream("C:\Documents and Settings\u13901\Desktop\Projects", IO.FileMode.Create, IO.FileAccess.Write)
fs.Write(b, 0, b.Length)
fs.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
推荐答案
我有一个为您编写的示例项目.
http://sdrv.ms/QjleCk
I have a sample project for you that I wrote.
http://sdrv.ms/QjleCk
这篇关于VB.Net代码以将MS Word文档插入并检索到MS SQL Server 2008中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!