Sql 我收到以下错误';条件表达式中的数据类型不匹配';添加数据时
我有以下错误,但我不确定为什么会出现此错误。当我尝试向数据库添加数据时,我收到此错误 条件表达式中的数据类型不匹配。 下面是我正在编写的代码。有人能帮忙吗?谢谢Sql 我收到以下错误';条件表达式中的数据类型不匹配';添加数据时,sql,vb.net,Sql,Vb.net,我有以下错误,但我不确定为什么会出现此错误。当我尝试向数据库添加数据时,我收到此错误 条件表达式中的数据类型不匹配。 下面是我正在编写的代码。有人能帮忙吗?谢谢 Private Sub RefreshData() If Not cnn.State = ConnectionState.Open Then 'open connection cnn.Open() End If Dim da As New OleDb.OleDbDataAdap
Private Sub RefreshData()
If Not cnn.State = ConnectionState.Open Then
'open connection
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT Item ID as [Item ID], " & _
"Item Name as [Item Name], " & _
"Item Type as [Item Type], " & _
"Quantity as [Quantity], " & _
"Min Shelf Stock as [Min Shelf Stock], " & _
"Purchase Price as [Purchase Price], Note " & _
" From Product ORDER BY Item ID", cnn)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
'offer data in data table into datagridview
Me.dgvData.DataSource = dt
'Close the connection
cnn.Close()
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
'The line of code below opens the connection to the database if it isnt open
cnn.Open()
End If
cmd.Connection = cnn
'Check whether to add new or update
If Me.txtItemID.Tag & "" = "" Then
'Add new
'The line of coding below adds data to table
cmd.CommandText = "INSERT INTO Product ([Item Id], [Item Name], [Item Type], [Quantity], [Min Shelf Stock], [Purchase Price], [Note]) VALUES (@id, @name, @iType, @quantity, @minshelfstock, @price, @note)"
cmd.Parameters.AddWithValue("@id", txtItemID.Text)
cmd.Parameters.AddWithValue("@name", txtItemName.Text)
cmd.Parameters.AddWithValue("@iType", cboItemType.Text)
cmd.Parameters.AddWithValue("@quantity", txtQuantity.Text)
cmd.Parameters.AddWithValue("@minshelfstock", txtMinShelfStock.Text)
cmd.Parameters.AddWithValue("@price", txtPurchasePrice.Text)
cmd.Parameters.AddWithValue("@note", txtNote.Text)
cmd.ExecuteNonQuery()
Else
'Update data in the table
cmd.CommandText = "UPDATE Product " & _
" SET [Item ID]=" & Me.txtItemID.Text & _
", [Item Name]='" & Me.txtItemName.Text & "'" & _
", [Item Type]='" & Me.cboItemType.Text & "'" & _
", [Quantity]=" & Me.txtQuantity.Text & "" & _
", [Min Shelf Stock]=" & Me.txtMinShelfStock.Text & "" & _
", [Purchase Price]=" & Me.txtPurchasePrice.Text & "" & _
", [Note]='" & Me.txtNote.Text & "'" & _
" WHERE [Item ID]=" & Me.txtItemID.Tag
cmd.ExecuteNonQuery()
End If
'Refresh data in list
RefreshData()
'Clear the form
Me.btnClear.PerformClick()
'The code below closes the connection to the database
cnn.Close()
End Sub
Private Sub Product_Load(sender As Object, e As EventArgs) Handles Me.Load
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\inventorysystem.mdb"
'
'Get the data into a list
Me.RefreshData()
End Sub
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
'check for the selected item in list
If Me.dgvData.Rows.Count > 0 Then
If Me.dgvData.SelectedRows.Count > 0 Then
Dim intItemID As Integer = Me.dgvData.SelectedRows(0).Cells("Item ID").Value
'Get the data from database followed Item ID
'Open the connection
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
'Get the data into the datatable
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Product " & _
" WHERE Item ID=" & intItemID, cnn)
Dim dt As New DataTable
da.Fill(dt)
Me.txtItemID.Text = intItemID
Me.txtItemName.Text = dt.Rows(0).Item("Item Name")
Me.cboItemType.Text = dt.Rows(0).Item("Item Type")
Me.txtQuantity.Text = dt.Rows(0).Item("Quantity")
Me.txtMinShelfStock.Text = dt.Rows(0).Item("Min Shelf Stock")
Me.txtPurchasePrice.Text = dt.Rows(0).Item("Purchase Price")
Me.txtNote.Text = dt.Rows(0).Item("Note")
'
'Hide the ID to be edited in TAG od txtItemID in case ID is changed
Me.txtItemID.Tag = intItemID
'Change the add button to update
Me.btnAdd.Text = "Update"
'Disable the Edit button
Me.btnEdit.Enabled = False
'Close the connection
cnn.Close()
End If
End If
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
'Check for the selected item in the list
If Me.dgvData.Rows.Count > 0 Then
If Me.dgvData.SelectedRows.Count > 0 Then
Dim intItemID As Integer = Me.dgvData.SelectedRows(0).Cells("Item ID").Value
'Open the connection
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
'Delete data
Dim cmd As New OleDb.OleDbCommand
cmd.Connection = cnn
cmd.CommandText = "DELETE FROM Product WHERE Item ID" & intItemID
cmd.ExecuteNonQuery()
'Refresh the data
Me.RefreshData()
'Close the connection
cnn.Close()
End If
End If
End Sub
最终类可能是由于数量数据类型造成的 试一试 不确定这是否是你想要达到的目标。问题在于方法。此方法创建参数及其数据类型。但是,要这样做,它会查看作为第二个参数传递的值的数据类型。如果该值是一个字符串(就像您总是传递文本框的Text属性那样),那么它将生成一个类型或类似的参数。当然,对于要更新的一个或多个字段,这不是数据库所期望的 您需要检查字段的数据类型,并传递类似数据类型的参数,对文本框的字符串值应用转换 例如,如果price字段的类型为numeric decimal,则需要写入
cmd.Parameters.AddWithValue("@price", Convert.ToDecimal(txtPurchasePrice.Text))
跳出页面的第一件事是项目ID、项目名称等的别名。您使用的是什么数据库?我是否能够像使用MS Access一样使用它,由于您只添加了要传递给命令的值的类型。它给了我一个错误“预期语句结束”。您能调试函数并查看它在哪里弹出错误消息吗?数据类型是:项目ID=编号,项目名称=文本,项目类型=文本,数量=编号,最小货架库存=编号,采购价格=货币,注意=Text,然后您需要将这些字段的文本框值转换为相应的数值。由于我不熟悉vb,请您为此编写代码,只需使用convert.ToInt32(textbox.Text)在access和Convert中整数/长型数字字段的参数值周围。对于十进制或Convert类型的数字字段,使用ToDecimal。对于双精度或Convert类型的数字字段,使用ToDouble。对于单精度类型的数字字段,使用ToSingle。出现以下错误:输入字符串的格式不正确
cmd.Parameters.AddWithValue("@price", Convert.ToDecimal(txtPurchasePrice.Text))