在一个控件中更新和插入语句

时间:2014-02-11 12:30:29

标签: vb.net ms-access

我的问题是在数据库中插入数据后没有。 tblBooklist中的副本不是一个副本。这是我的示例代码:

      Dim sqlinsert As String
      sqlinsert = "UPDATE tblBookList SET Copy = Copy - 1 WHERE 
      [Accession Number] = @Accession_Number"
      sqlinsert = "INSERT INTO tblBookIssue([Accession Number],[Book Title],
      [Student Name],[Years],[Section],[Date Issue],[Time],[Am or Pm],[Date to be Return], 
      [Status])  VALUES (@Accession_Number,@Book_Title,@Student_Name,@Years,
      @Section,@Date_Issue,@Time,@Am_or_Pm,@Date_to_be_Return,@Status)"

      Dim cmd As New OleDbCommand(sqlinsert, con)
      cmd.Parameters.Add(New OleDbParameter("@Accession_Number", txtBookAccessNumber.Text))
      cmd.Parameters.Add(New OleDbParameter("@Book_Title", txtBookBookTitle.Text))
      cmd.Parameters.Add(New OleDbParameter("@Student_Name", txtBookStudentName.Text))
      cmd.Parameters.Add(New OleDbParameter("@Years", txtBookYears.Text))
      cmd.Parameters.Add(New OleDbParameter("@Section", txtBookSection.Text))
      cmd.Parameters.Add(New OleDbParameter("@Date_Issue", Label88.Text))
      cmd.Parameters.Add(New OleDbParameter("@Time", lblTime.Text))
      cmd.Parameters.Add(New OleDbParameter("@Am_or_Pm", lblAm.Text))
      cmd.Parameters.Add(New OleDbParameter("@Date_to_be_Return", 
      txtBookDatetobeReturn.Text))
      cmd.Parameters.Add(New OleDbParameter("@Status", lblStatus.Text))
      con.Open()
      cmd.ExecuteNonQuery()

      MessageBox.Show("Book has been borrowed", "Issue", MessageBoxButtons.OK, 
      MessageBoxIcon.Information)

提前感谢您的帮助

2 个答案:

答案 0 :(得分:1)

正如OTTA已经指出的,这是更正后的代码:

  Dim sqlinsert As String
  sqlinsert = "UPDATE tblBookList SET Copy = Copy - 1 WHERE 
  [Accession Number] = @Accession_Number"
  Dim cmd As New OleDbCommand(sqlinsert, con)
  cmd.Parameters.Add(New OleDbParameter("@Accession_Number", txtBookAccessNumber.Text))
  cmd.ExecuteNonQuery()

  sqlinsert = "INSERT INTO tblBookIssue([Accession Number],[Book Title],
  [Student Name],[Years],[Section],[Date Issue],[Time],[Am or Pm],[Date to be Return], 
  [Status])  VALUES (@Accession_Number,@Book_Title,@Student_Name,@Years,
  @Section,@Date_Issue,@Time,@Am_or_Pm,@Date_to_be_Return,@Status)"
  cmd.CommandText = sqlinsert
  cmd.Parameters.Add(New OleDbParameter("@Book_Title", txtBookBookTitle.Text))
  cmd.Parameters.Add(New OleDbParameter("@Student_Name", txtBookStudentName.Text))
  cmd.Parameters.Add(New OleDbParameter("@Years", txtBookYears.Text))
  cmd.Parameters.Add(New OleDbParameter("@Section", txtBookSection.Text))
  cmd.Parameters.Add(New OleDbParameter("@Date_Issue", Label88.Text))
  cmd.Parameters.Add(New OleDbParameter("@Time", lblTime.Text))
  cmd.Parameters.Add(New OleDbParameter("@Am_or_Pm", lblAm.Text))
  cmd.Parameters.Add(New OleDbParameter("@Date_to_be_Return", 
  txtBookDatetobeReturn.Text))
  cmd.Parameters.Add(New OleDbParameter("@Status", lblStatus.Text))
  cmd.ExecuteNonQuery()

  MessageBox.Show("Book has been borrowed", "Issue", MessageBoxButtons.OK, 
  MessageBoxIcon.Information)

答案 1 :(得分:0)

你的Update语句永远不会被执行,你首先要做的是将sqlinsert设置为你的Update语句,接下来你要用你的Insert语句覆盖它然后执行。