2017-07-27 202 views
0

我有一个简单的二十一点游戏,我试图通过添加2个简单的值来更新数据库。我希望它将用户名和玩家的分数添加到数据库中。大酒杯项目无法更新Access数据库

看的在线教程后,这是我试过的代码:

Imports System.Data.OleDb 

Public Class Form1 

    'for updating database 
    Dim provider As String 
    Dim dataFile As String 
    Dim connString As String 
    Dim myConnection As OleDbConnection = New OleDbConnection 

    Private Sub ButtonSaveScore_Click(sender As Object, e As EventArgs) Handles ButtonSaveScore.Click 

     provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" 
     dataFile = "F:\Documents\Class Documents\CSC289 - K6A - Programming Capstone Project\Project\BlackJack\BlackJack\Scoreboard.accdb" 
     connString = provider & dataFile 
     myConnection.ConnectionString = connString 
     myConnection.Open() 
     Dim str As String 
     str = "Update [Scores] set [UserName] = '" & playerName & "',[Score] =' " & wins & "' where [ID] = NEW" 
     Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection) 

     Try 
      cmd.ExecuteNonQuery() 
      cmd.Dispose() 
      myConnection.Close() 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 

    End Sub 

我得到的错误:

one or more variables is not provided

我第一次打的保存按钮。第二次它打破了应用程序,它说我“不能调整文件目前的状态已经打开,然后它突出myConnection.ConnectionString = connString

+0

你忘了'around'NEW',所以它认为它是一个列名,对于访问表中没有找到的所有名称都是参数。 – litelite

+0

此外,您的代码有[SQL注入](https://stackoverflow.com/questions/601300/what-is-sql-injection)的风险,您应该使用具有适当参数的预准备语句。 – litelite

+0

你的第二个问题。这是因为您必须在尝试重新打开数据库之前关闭数据库。目前只有在查询没有抛出任何东西时才关闭数据库。您应该将连接关闭到['Finally'块](https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement)。或者,您可以使用['使用'块](https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-statement) – litelite

回答

1

考虑实施Using:。

Sometimes your code requires an unmanaged resource, such as a file handle, a COM wrapper, or a SQL connection. A Using block guarantees the disposal of one or more such resources when your code is finished with them. This makes them available for other code to use.

下面是一些示例代码:

Using con As New OleDbConnection(connectionString), 
     cmd As New OleDbCommand(commandString, con) 

    con.Open() 

    cmd.ExecuteNonQuery() 

End Using 

这将处理关闭和你的对象的处置。

我也会考虑使用参数来避免SQL注入。有关详细信息,请参阅Bobby Tables

下面是一些示例代码:

Using con As New OleDbConnection(connectionString), 
     cmd As New OleDbCommand("UPDATE [Scores] SET [UserName] = ?, [Score] = ? WHERE [ID] = ?", con) 

    con.Open() 

    cmd.Parameters.Add("@Username", OleDbType.[Type]).Value = playerName 
    cmd.Parameters.Add("@Score", OleDbType.[Type]).Value = wins 

    'I've popped this into a parameter as I'm unsure what it is. I'll leave that for you to decide 
    cmd.Parameters.Add("@ID", OleDbType.[Type]).Value = "NEW" 

    cmd.ExecuteNonQuery() 

End Using 

Note that I have used OleDbType.[Type] . You will want to replace this with the data type you have specified for your columns.

随着OleDb你参数的顺序是非常重要的,而不是命名。确保您创建参数,如上所示,它们出现在您的命令中。