获取图像源并保存到数据库

时间:2019-06-14 08:42:15

标签: asp.net vb.net

我有一幅在搜索后填充的图像,一旦它进入图像控件,我现在想将其插入数据库中。

这是我的代码,用于将所选数据提交到数据库中。我在cmd.Parameters.AddWithValue(“ @ Photo”,image1)行上遇到错误


 Protected Sub btnSubmit_Click(sender As Object, e As EventArgs)

     Dim d As New Date(2019, 5, 16)
     d = DateAdd(DateInterval.Day, 365, d)

     Dim currentID As Integer
     Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
     Dim contentType As String = FileUpload1.PostedFile.ContentType
     Dim fs As Stream = FileUpload1.PostedFile.InputStream
     Dim br As New BinaryReader(fs)
     Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Integer))

     'Try

        If txtClockNo.Text <> "" And txtDepartment.Text <> "" And txtName.Text <> "" And txtIDNo.Text <> "" And txtSurname.Text <> "" And txtCostCentre.Text <> "" Then

            Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("##").ToString())

                Using cmd As New SqlCommand("INSERT INTO MedicalCertificate (Code,EmployeeID,FirstName,LastName,DateOfMedical,DateOfUpload,Certificate,UploadType,Photo)  VALUES (@Code,@EmployeeID,@FirstName,@LastName,@DateOfMedical,@DateOfUpload,@Certificate,@UploadType,@Photo);SELECT SCOPE_IDENTITY()", con)             

                    cmd.Parameters.AddWithValue("@Code", txtClockNo.Text)
                    cmd.Parameters.AddWithValue("@EmployeeID", txtIDNo.Text)
                    cmd.Parameters.AddWithValue("@FirstName", txtName.Text)
                    cmd.Parameters.AddWithValue("@LastName", txtSurname.Text)
                    cmd.Parameters.AddWithValue("@DateOfMedical", DateTime.Today())
                    cmd.Parameters.AddWithValue("@DateOfUpload", DateTime.Today())
                    cmd.Parameters.AddWithValue("@Certificate", bytes)
                    cmd.Parameters.AddWithValue("@UploadType", contentType)
                    cmd.Parameters.AddWithValue("@Photo", image1)

                    con.Open()
                    cmd.ExecuteNonQuery()
                    currentID = cmd.ExecuteScalar()

                    con.Close()

                End Using

End Sub

0 个答案:

没有答案