将图像添加到Access Database Vb.net - 插入语法错误

时间:2016-04-03 17:15:51

标签: sql database vb.net visual-studio ms-access

我是Visual Basic的新手。我正在开发一个简单的学生注册系统,其中包含带有照片的学生档案。我设法在线获取图像添加代码,我试图找出它并将其用于我的程序。除了将插入照片插入数据库“Students”表的“Image”字段的sql insert语句外,全部设置。当我单击“保存”按钮时,我不断收到“插入语法”错误,但我似乎无法确切地知道sql语句中的错误是什么。如果我能得到帮助,我将不胜感激。这是配置文件的整个代码,但是我已经在sql语句中放置了注释,所以你可以直接在那里。再次感谢并再次感谢

    Imports System.IO
    Imports System.Data.OleDb

     Public Class Profile
      Dim con As New OleDb.OleDbConnection
      Dim ds As New DataSet
      Dim da As New OleDb.OleDbDataAdapter
      Dim sql As String
      Dim conString As String
      Dim inc As Integer
      Dim maxrows As Integer
      Dim dsNewRow As DataRow
      Dim SID As String

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles txtNationality.SelectedIndexChanged

End Sub

Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter

End Sub
Private Sub ProfileRecords()
    txtFirstName.Text = ds.Tables("StudentRegSYS").Rows(inc).Item(1)
    txtMiddleName.Text = ds.Tables("StudentRegSYS").Rows(inc).Item(2)
    txtLastName.Text = ds.Tables("StudentRegSYS").Rows(inc).Item(3)
    txtGender.Text = ds.Tables("StudentRegSYS").Rows(inc).Item(4)
    txtNationality.Text = ds.Tables("StudentRegSYS").Rows(inc).Item(5)
    txtLevel.Text = ds.Tables("StudentRegSYS").Rows(inc).Item(6)
    txtFaculty.Text = ds.Tables("StudentRegSYS").Rows(inc).Item(7)
    txtProgramme.Text = ds.Tables("StudentRegSYS").Rows(inc).Item(8)
    txtAcademicPeriod.Text = ds.Tables("StudentRegSYS").Rows(inc).Item(9)
    txtDateRegistered.Text = ds.Tables("StudentRegSYS").Rows(inc).Item(10)

    maxrows = ds.Tables("StudentRegSYS").Rows.Count

    txtFirstName.ReadOnly = True
    txtMiddleName.ReadOnly = True
    txtLastName.ReadOnly = True
    txtGender.Enabled = False
    txtFaculty.Enabled = False
    txtLevel.ReadOnly = True
    txtNationality.Enabled = False
    txtProgramme.Enabled = False
    txtAcademicPeriod.ReadOnly = True
    txtDateRegistered.ReadOnly = True



End Sub

Private Sub Profile_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    SID = Login.txtstudentid.Text
    btneditcancel.Visible = False
    btneditsave.Visible = False
    btnbrowseimage.Visible = False
    txtimagepath.Visible = False

    Try
        conString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Yaw Biney\Documents\Visual Studio 2015\Projects\Student Registration System\Student Registration System\StudentRegSYS.accdb"
        con = New OleDbConnection(conString)
        con.Open()
        sql = "select * from Students Where SID='" + SID + "'"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "StudentRegSYS")
        con.Close()

    Catch EXP As Exception
        MessageBox.Show(EXP.ToString)
    End Try
    con.Open()

    ProfileRecords()

    txtNationality.Items.Add("Ghanaian")
    txtNationality.Items.Add("Togolese")
    txtNationality.Items.Add("Gabonese")
    txtNationality.Items.Add("Ivorian")
    txtNationality.Items.Add("Burkinabe")
    txtNationality.Items.Add("Nigerian")
    txtNationality.Items.Add("Beninois")


    txtFaculty.Items.Add("Informatics")
    txtFaculty.Items.Add("Engineering")
    txtFaculty.Items.Add("Business")

    txtProgramme.Items.Add("Bsc. Information Technology")
    txtProgramme.Items.Add("Bsc. Telecom Engineering")
    txtProgramme.Items.Add("Bsc. Computer Engineering")
    txtProgramme.Items.Add("BA. Business Administration")
    txtProgramme.Items.Add("Diploma in Information Technology")
    txtProgramme.Items.Add("Diploma in Business Studies")


    txtwelcomemsg.Text = "Welcome " & txtLastName.Text & ", " & txtFirstName.Text & " " & txtMiddleName.Text

End Sub


Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles txtMiddleName.TextChanged

End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    btneditcancel.Visible = True
    btneditsave.Visible = True
    btnbrowseimage.Visible = True
    txtimagepath.Visible = True

    txtFirstName.ReadOnly = False
    txtMiddleName.ReadOnly = False
    txtLastName.ReadOnly = False
    txtGender.Enabled = True
    txtFaculty.Enabled = True
    txtLevel.ReadOnly = False
    txtNationality.Enabled = True
    txtProgramme.Enabled = True
    txtAcademicPeriod.ReadOnly = False
    txtDateRegistered.ReadOnly = False
End Sub

Private Sub btneditcancel_Click(sender As Object, e As EventArgs) Handles btneditcancel.Click

    btneditcancel.Visible = False
    btneditsave.Visible = False
    btnbrowseimage.Visible = False
    txtimagepath.Visible = False

    txtFirstName.ReadOnly = True
    txtMiddleName.ReadOnly = True
    txtLastName.ReadOnly = True
    txtGender.Enabled = False
    txtFaculty.Enabled = False
    txtLevel.ReadOnly = True
    txtNationality.Enabled = False
    txtProgramme.Enabled = False
    txtAcademicPeriod.ReadOnly = True
    txtDateRegistered.ReadOnly = True
End Sub

Private Sub btneditsave_Click(sender As Object, e As EventArgs) Handles btneditsave.Click
    Dim cb As New OleDb.OleDbCommandBuilder(da)


    Try
        ds.Tables("StudentRegSYS").Rows(inc).Item(1) = txtFirstName.Text
        ds.Tables("StudentRegSYS").Rows(inc).Item(2) = txtMiddleName.Text
        ds.Tables("StudentRegSYS").Rows(inc).Item(3) = txtLastName.Text
        ds.Tables("StudentRegSYS").Rows(inc).Item(4) = txtGender.Text
        ds.Tables("StudentRegSYS").Rows(inc).Item(5) = txtNationality.Text
        ds.Tables("StudentRegSYS").Rows(inc).Item(6) = txtLevel.Text
        ds.Tables("StudentRegSYS").Rows(inc).Item(7) = txtFaculty.Text
        ds.Tables("StudentRegSYS").Rows(inc).Item(8) = txtProgramme.Text
        ds.Tables("StudentRegSYS").Rows(inc).Item(9) = txtAcademicPeriod.Text
        ds.Tables("StudentRegSYS").Rows(inc).Item(10) = txtDateRegistered.Text


        da.Update(ds, "StudentRegSYS")
        MessageBox.Show("Profile updated successfully")

    Catch ex As Exception
        MessageBox.Show("Error: " & ex.ToString())
    End Try




     'Error seems to be from here on going'
    Try
        Dim MemStream As New MemoryStream
        Dim DataPic_Update As Byte()
        Me.picboxprofilepic.Image.Save(MemStream, Imaging.ImageFormat.Jpeg)
        DataPic_Update = MemStream.GetBuffer()
        MemStream.Read(DataPic_Update, 0, MemStream.Length)
        Dim cmd As OleDbCommand = New OleDbCommand("INSERT INTO Students (Image) VALUES(@Image)", con)

        Dim photo As OleDbParameter = New OleDbParameter("@Image", SqlDbType.Image)
        photo.Value = DataPic_Update
        cmd.Parameters.Add(photo)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Record Is Added")
    Catch ex As Exception
        MessageBox.Show("Error: " & ex.ToString())
    End Try

    btneditcancel.Visible = False
    btneditsave.Visible = False
    btnbrowseimage.Visible = False
    txtimagepath.Visible = False

    txtFirstName.ReadOnly = True
    txtMiddleName.ReadOnly = True
    txtLastName.ReadOnly = True
    txtGender.Enabled = False
    txtFaculty.Enabled = False
    txtLevel.ReadOnly = True
    txtNationality.Enabled = False
    txtProgramme.Enabled = False
    txtAcademicPeriod.ReadOnly = True
    txtDateRegistered.ReadOnly = True
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Change_Password.Show()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnbrowseimage.Click

    OpenFileDialog1.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*"

    If OpenFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
        Me.picboxprofilepic.Image = System.Drawing.Image.FromFile(OpenFileDialog1.FileName)
        txtimagepath.Text = OpenFileDialog1.FileName
    End If

End Sub
   End Class

0 个答案:

没有答案