2015-12-18 133 views
0

雖然代碼不顯示任何錯誤,但當我去檢查數據庫中的表時,這些值不會顯示。爲什麼我的值不能插入到我的Access數據庫中?

If Feedbacktxt.Text = "" Then 
     MsgBox("You cannot submit empty feedback") 
Else 

    provider = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source="  'Telling your program what data source we're using 

    datafile = "D:\HistoryDatabase\HistoryDatabase.accdb" 'The location of the datafile to where 

    connectstring = provider & datafile 

    myconnection.ConnectionString = connectstring 'An object that allows us to connect to the microsoft connection 

    myconnection.Open() 'Opens up the database file 

    Dim SQLInsert As String = "INSERT INTO AccountNames([TeacherFeedBack]) VALUES ('" & Feedbacktxt.Text & "') where [Username] = " & NameComboBox.SelectedItem 
    Dim cmd2 As OleDbCommand = New OleDbCommand(SQLInsert, myconnection) 
    Feedbacktxt.Clear() 
    MsgBox("Your feedback has been submitted") 

End If 
+0

這是否'昏暗CMD2作爲OleDbCommand的=新OleDbCommand(SQLInsert,myconnection)'執行命令? 不應該在創建OleDbComamnd實例的行後面使用'¨leDbCommand.ExecuteNonQuery'嗎? – DukeOfHazard

+1

你可以檢查這個http://stackoverflow.com/questions/2228079/checking-if-insert-into-statement-was-successful – DukeOfHazard

+1

我認爲你應該使用'update'查詢,因爲'插入'查詢沒有where子句。並且知道這些查詢返回受影響的行數。 – Yog

回答

0

您的SQL錯誤,將數據插入到數據庫時,你不需要在這種情況下WHERE條款。

Dim SQLInsert As String = "INSERT INTO AccountNames([TeacherFeedBack]) VALUES ('" & Feedbacktxt.Text & "')" 

嘗試修復該行或進一步解釋。

0

第一個例子和第二個例子中由命令文本

的創建不同使用XML文本

Public Function AddNewAccount(ByVal TeacherFeedBack As String) As Integer 
    Dim newPrimaryKey As Integer = 0 

    ' easily create connection string w/o string concatenation 
    Dim Builder As New OleDb.OleDbConnectionStringBuilder With 
     { 
      .Provider = "Microsoft.ACE.OLEDB.12.0", 
      .DataSource = "D:\HistoryDatabase\HistoryDatabase.accdb" 
     } 

    Using cn As New OleDb.OleDbConnection With 
     { 
      .ConnectionString = Builder.ConnectionString 
     } 
     Using cmd As New OleDb.OleDbCommand With {.Connection = cn} 
      ' use xml literal for creating nicely formatted query 
      ' use parameter which applies proper formatting for the 
      ' value, in this case a string and if there are embedded 
      ' apostrophe will properly escape them 
      cmd.CommandText = 
       <SQL> 
        INSERT INTO AccountNames 
        (
         TeacherFeedBack 
        ) 
        Values 
        (
         @TeacherFeedBack 
        ) 
       </SQL>.Value 

      ' 
      ' Set value for above parameter. Some will say to use 
      ' ? rather than a named parameter but named parameters are 
      ' great when using many parameters. Named parameters need 
      ' to be in the same order as the elements in the values part 
      ' of the query. 
      ' 
      cmd.Parameters.AddWithValue("@TeacherFeedBack", TeacherFeedBack) 
      cn.Open() 
      ' execute insert query, check if one was actually added 
      If cmd.ExecuteNonQuery() = 1 Then 
       ' 
       ' Get new primary key for record just added above 
       ' 
       cmd.CommandText = "Select @@Identity" 
       newPrimaryKey = CInt(cmd.ExecuteScalar) 
      End If 
     End Using 
    End Using 

    Return newPrimaryKey 

End Function 

沒有XML文本

Public Function AddNewAccount(ByVal TeacherFeedBack As String) As Integer 
    Dim newPrimaryKey As Integer = 0 

    ' easily create connection string w/o string concatenation 
    Dim Builder As New OleDb.OleDbConnectionStringBuilder With 
     { 
      .Provider = "Microsoft.ACE.OLEDB.12.0", 
      .DataSource = "D:\HistoryDatabase\HistoryDatabase.accdb" 
     } 

    Using cn As New OleDb.OleDbConnection With 
     { 
      .ConnectionString = Builder.ConnectionString 
     } 
     Using cmd As New OleDb.OleDbCommand With {.Connection = cn} 
      ' use xml literal for creating nicely formatted query 
      ' use parameter which applies proper formatting for the 
      ' value, in this case a string and if there are embedded 
      ' apostrophe will properly escape them 
      cmd.CommandText = "INSERT INTO AccountNames (TeacherFeedBack) Values (@TeacherFeedBack)" 

      ' 
      ' Set value for above parameter. Some will say to use 
      ' ? rather than a named parameter but named parameters are 
      ' great when using many parameters. Named parameters need 
      ' to be in the same order as the elements in the values part 
      ' of the query. 
      ' 
      cmd.Parameters.AddWithValue("@TeacherFeedBack", TeacherFeedBack) 
      cn.Open() 
      ' execute insert query, check if one was actually added 
      If cmd.ExecuteNonQuery() = 1 Then 
       ' 
       ' Get new primary key for record just added above 
       ' 
       cmd.CommandText = "Select @@Identity" 
       newPrimaryKey = CInt(cmd.ExecuteScalar) 
      End If 
     End Using 
    End Using 

    Return newPrimaryKey 

End Function 
相關問題