当我通过图片框将图片插入我的数据库时,我得到一个例外

i am getting the following exception while i am inserting a picture into database through a picturebox

System.IO.IOException was unhandled
Message=The process cannot access the file 'C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg' because it is being used by another process.


i have tried the following code:










Imports System.IO
02
Imports System.Data
03
Imports System.Data.SqlClient
04
Imports System.Data.SqlTypes
05
Public Class Form1
06
    Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=ImagesStore;Integrated Security=True;Pooling=False")
07
    Dim cmd As SqlCommand
08
 
09
    Private mImageFile As Image
10
    Private mImageFilePath As String
11
 
12
 Private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpen.Click
13
        Dim imge As String
14
        OpenFileDialog1.Title = "Set Image File"
15
        OpenFileDialog1.Filter = "Bitmap Files|*.bmp" & _
16
            "|Gif Files|*.gif|JPEG Files|*.jpg"
17
        OpenFileDialog1.DefaultExt = "bmp"
18
        OpenFileDialog1.FilterIndex = 1
19
        OpenFileDialog1.FileName = ""
20
        OpenFileDialog1.ShowDialog()
21
 
22
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
23
            Exit Sub
24
        End If
25
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
26
            imge = OpenFileDialog1.FileName
27
            PictureBox1.Image = System.Drawing.Bitmap.FromFile(imge)
28
        End If
29
        Dim sFilePath As String
30
        sFilePath = OpenFileDialog1.FileName
31
        If sFilePath = "" Then Exit Sub
32
 
33
        If System.IO.File.Exists(sFilePath) = False Then
34
            Exit Sub
35
        Else
36
            txtImageFile.Text = sFilePath
37
            mImageFilePath = sFilePath
38
        End If
39
    End Sub
40
 
41
 Private Sub BtnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnInsert.Click
42
        Try
43
            If (Me.txtImageFile.Text = String.Empty Or Me.txtTitle.Text =
44
                String.Empty) Then
45
                MessageBox.Show("Complete both form fields prior to submitting",
46
                "Missing Values", _
47
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
48
                Exit Sub
49
            End If
50
        Catch ex As Exception
51
            MessageBox.Show(ex.Message.ToString(), "File Test Error")
52
        End Try
53
 
54
        Dim fs As FileStream = New FileStream(mImageFilePath.ToString(),
55
        FileMode.Open)    \\I am getting the exception in this line \\
56
 
57
        Dim img As Byte() = New Byte(fs.Length) {}
58
        fs.Read(img, 0, fs.Length)
59
        fs.Close()
60
 
61
        Dim sSQL As String = "INSERT INTO ImagesStore(OriginalPath,ImageData)VALUES(@op,@id)"
62
        cmd = New SqlCommand(sSQL, conn)
63
 
64
        cmd.Parameters.AddWithValue("@op", (img))
65
        cmd.Parameters.AddWithValue("@id", (txtTitle.Text))
66
 
67
        Try
68
            conn.Open()
69
            cmd.ExecuteNonQuery()
70
            conn.Close()
71
            MessageBox.Show("Query executed.", "Image Load")
72
        Catch ex As Exception
73
            MessageBox.Show(ex.Message.ToString(), "Data Error")
74
            Exit Sub
75
        End Try
76
    End Sub



 1 条回答