当我更新数据库时,当我插入..... vb.net时,信息不会被插入

时间:2016-05-24 07:38:52

标签: database vb.net access

我遇到了一个大问题。

当我更新数据库时,信息不会插入数据库。但是当我将信息插入数据库时​​,它不会更新。

这是我更新数据库时尝试插入的代码,但它不起作用,只有更新:

 provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\311026\Documents\PAP3\Biblio.accdb"
   connString = provider
   myConnection.ConnectionString = connString
   myConnection.Open()
   Dim str As String

   str = "Insert into Livrosrequisitar([NomeLivro], [AutorLivro], [EditoraLivro], [AnoLivro], [Nome_Aluno], [Ano_Aluno], [Turma_Aluno], [Contacto_Aluno], [Email_Aluno], [DataRequicicao], [RetornoRequicicao]) Values (?,?,?,?,?,?,?,?,?,?,?)"
   Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)

   cmd.Parameters.Add(New OleDbParameter("NomeLivro", CType(TextBox11.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("AutorLivro", CType(TextBox4.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("EditoraLivro", CType(TextBox5.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("AnoLivro", CType(TextBox6.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("Nome_Aluno", CType(TextBox10.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("Ano_Aluno", CType(TextBox9.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("Turma_Aluno", CType(TextBox8.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("Contacto_Aluno", CType(TextBox13.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("Email_Aluno", CType(TextBox12.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("DataRequicicao", CType(DateTimePicker1.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("RetornoRequicicao", CType(DateTimePicker2.Text, String)))

   cmd = New OleDb.OleDbCommand("UPDATE Livrosescola SET Quantidade = Quantidade - 1 Where NomeLivro like'%" & ListBox1.SelectedValue & "%'", myConnection)
   ListBox1.Refresh()

   Try
       cmd.ExecuteNonQuery()
       cmd.Dispose()
       myConnection.Close()
       TextBox11.Clear()
       TextBox4.Clear()
       TextBox5.Clear()
       TextBox6.Clear()
       TextBox10.Clear()
       TextBox9.Clear()
       TextBox8.Clear()
       TextBox13.Clear()
       TextBox12.Clear()
       TextBox9.Clear()

       MessageBox.Show("Registo inserido!", "Registo inserido", MessageBoxButtons.OK, MessageBoxIcon.Information)

   Catch ex As Exception
       MessageBox.Show("Erro!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
   End Try

   listbox() 

这是我在插入信息时尝试更新的代码,但只插入代码。

provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\311026\Documents\PAP3\Biblio.accdb"
   connString = provider
   myConnection.ConnectionString = connString
   myConnection.Open()
   Dim str As String

   str = "Insert into Livrosrequisitar([NomeLivro], [AutorLivro], [EditoraLivro], [AnoLivro], [Nome_Aluno], [Ano_Aluno], [Turma_Aluno], [Contacto_Aluno], [Email_Aluno], [DataRequicicao], [RetornoRequicicao]) Values (?,?,?,?,?,?,?,?,?,?,?)"
   Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)


   cmd.Parameters.Add(New OleDbParameter("NomeLivro", CType(TextBox11.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("AutorLivro", CType(TextBox4.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("EditoraLivro", CType(TextBox5.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("AnoLivro", CType(TextBox6.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("Nome_Aluno", CType(TextBox10.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("Ano_Aluno", CType(TextBox9.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("Turma_Aluno", CType(TextBox8.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("Contacto_Aluno", CType(TextBox13.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("Email_Aluno", CType(TextBox12.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("DataRequicicao", CType(DateTimePicker1.Text, String)))
   cmd.Parameters.Add(New OleDbParameter("RetornoRequicicao", CType(DateTimePicker2.Text, String)))



   Try
       cmd.ExecuteNonQuery()
       cmd.Dispose()
       myConnection.Close()
       TextBox11.Clear()
       TextBox4.Clear()
       TextBox5.Clear()
       TextBox6.Clear()
       TextBox10.Clear()
       TextBox9.Clear()
       TextBox8.Clear()
       TextBox13.Clear()
       TextBox12.Clear()
       TextBox9.Clear()

       MessageBox.Show("Registo inserido!", "Registo inserido", MessageBoxButtons.OK, MessageBoxIcon.Information)

   Catch ex As Exception
       MessageBox.Show("Erro!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
   End Try

   cmd = New OleDb.OleDbCommand("UPDATE Livrosescola SET Quantidade = Quantidade - 1 Where NomeLivro like'%" & ListBox1.SelectedValue & "%'", myConnection)
   ListBox1.Refresh()

   listbox() 

我想同时做同样的事情,但它不起作用......

如果你能帮助我......

谢谢!

1 个答案:

答案 0 :(得分:2)

需要执行命令才能执行任何操作。

在你的代码的两个例子中,你只为一个命令文本而不是另一个命令文本调用ExecuteNonQuery。

在第一个例子中,只需添加以下行:

......
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)

Try
    ' Add this (and move the start of the Try catch before this line'
     cmd.ExecuteNonQuery()

    cmd.Parameters.Add(New OleDbParameter("NomeLivro", CType(TextBox11.Text, String)))
    ....
Catch ex as Exception
    MessageBox.Show("Error:" & ex.Message)
End Try

在第二个示例中,您遇到了同样的问题,但在最后一次创建OleDbCommand后缺少ExecuteNonQuery