第一個例子和第二個例子中由命令文本
的創建不同使用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
這是否'昏暗CMD2作爲OleDbCommand的=新OleDbCommand(SQLInsert,myconnection)'執行命令? 不應該在創建OleDbComamnd實例的行後面使用'¨leDbCommand.ExecuteNonQuery'嗎? – DukeOfHazard
你可以檢查這個http://stackoverflow.com/questions/2228079/checking-if-insert-into-statement-was-successful – DukeOfHazard
我認爲你應該使用'update'查詢,因爲'插入'查詢沒有where子句。並且知道這些查詢返回受影響的行數。 – Yog