关于网友提出的“parameters.add Parameters用法的问题”问题疑问,本网通过在网上对“parameters.add Parameters用法的问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:parameters.add Parameters用法的问题描述:
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("App_Data\Northwind.mdb")
Dim strSQL As String = "update Categories set Description = @Description where CategoryID = @CategoryID"
Dim oConn As OleDbConnection = New OleDbConnection(strConn)
Dim oComm As OleDbCommand = New OleDbCommand(strSQL, oConn)
oComm.Parameters.Add("@Description", OleDbType.LongVarChar)
oComm.Parameters("@Description").Value = "nbvbbbbbbbbb" 'TextBox2.Text
oComm.Parameters.Add("@CategoryID", OleDbType.Integer, 15)
oComm.Parameters("@CategoryID").Value = 23 ' CInt(TextBox1.Text)
oConn.Open()
oComm.ExecuteNonQuery()
Try
Catch ex As Exception
'错误处理代码
Finally
'一定执行的代码
oConn.Close()
End Try
以上程序执行是没问题的 可是要是把它改成这样的,就有问题了,问题不大,只是不明白。
oComm.Parameters.Add("@Description", OleDbType.LongVarChar)
oComm.Parameters("@Description").Value = "nbvbbbbbbbbb" 'TextBox2.Text
oComm.Parameters.Add("@CategoryID", OleDbType.Integer, 15)
oComm.Parameters("@CategoryID").Value = 23 ' CInt(TextBox1.Text)
改成:
oComm.Parameters.Add("@CategoryID", OleDbType.Integer, 15)
oComm.Parameters("@CategoryID").Value = 23 ' CInt(TextBox1.Text)
oComm.Parameters.Add("@Description", OleDbType.LongVarChar)
oComm.Parameters("@Description").Value = "nbvbbbbbbbbb" 'TextBox2.Text
然后就不会更改数据库的记录,也不报错,不知道是什么原因。知道的说一下
解决方案1:
Access不支持命名参数,只按顺序识别参数...
解决方案2: 用法的问题。你可以直接这样写:
oComm.Parameters.AddWithValue("@Description", TextBox1.Text)
这样也可以使用,但是不建议这样写