当我单击Update
按钮时,将调用以下功能,但数据库未更新。语法是正确的,但我无法弄清楚我缺少什么。有什么建议吗?
Private Sub UpDateRecord()
Dim connString As String = ConfigurationManager.ConnectionStrings("dbx").ConnectionString
Dim cmdString As String = "UPDATE hpn " & _
"SET " &_
"Day = @Day, " & _
"FullName =@FullName, " & _
"Balance = @Balance, " & _
"Deposit = @Deposit, " & _
"Withdraw = @Withdraw, " & _
"CashIn = @CashIn, " & _
"CashOut = @CashOut, " & _
"Spend = @Spend, " & _
"Total = @Total " & _
"WHERE ID = @ID"
Using conn As New OleDbConnection(connString)
conn.Open() 'conn.Open() before creating OleDbCommand Obj
Using cmd As New OleDbCommand(cmdString, conn)
cmd.Parameters.AddWithValue("@Day", TextBox1.Text)
cmd.Parameters.AddWithValue("@FullName", TextBox2.Text)
cmd.Parameters.AddWithValue("@Balance", TextBox3.Text)
cmd.Parameters.AddWithValue("@Deposit", TextBox4.Text)
cmd.Parameters.AddWithValue("@Withdraw", TextBox5.Text)
cmd.Parameters.AddWithValue("@CashIn", TextBox6.Text)
cmd.Parameters.AddWithValue("@CashOut", TextBox7.Text)
cmd.Parameters.AddWithValue("@Spend", TextBox8.Text)
cmd.Parameters.AddWithValue("@Total", TextBox9.Text)
cmd.Parameters.AddWithValue("@ID", Me.ID)
End Using
End Using
End Sub
答案 0 :(得分:2)
如果您不调用OleDbCommand.ExecuteNonQuery()
,应该如何更新数据库?
固定代码:
Using conn As New OleDbConnection(connString)
conn.Open() 'conn.Open() before creating OleDbCommand Obj
Using cmd As New OleDbCommand(cmdString, conn)
cmd.Parameters.AddWithValue("@Day", TextBox1.Text)
cmd.Parameters.AddWithValue("@FullName", TextBox2.Text)
cmd.Parameters.AddWithValue("@Balance", TextBox3.Text)
cmd.Parameters.AddWithValue("@Deposit", TextBox4.Text)
cmd.Parameters.AddWithValue("@Withdraw", TextBox5.Text)
cmd.Parameters.AddWithValue("@CashIn", TextBox6.Text)
cmd.Parameters.AddWithValue("@CashOut", TextBox7.Text)
cmd.Parameters.AddWithValue("@Spend", TextBox8.Text)
cmd.Parameters.AddWithValue("@Total", TextBox9.Text)
cmd.Parameters.AddWithValue("@ID", Me.ID)
cmd.ExecuteNonQuery() 'THIS IS THE LINE YOU WERE MISSING
End Using
conn.Close() 'YOU FORGOT TO CLOSE THE CONNECTION
End Using
这段代码充满了错误:
Using
内打开连接OleDbCommand.ExecuteNonQuery()
,因此所有参数均已设置,但未执行查询。