我正在使用带有OleDb
数据库的vb.net。
以下是我的代码,虽然我要搜索数据库,但在进行更改后无法保存。
它在Command.ExecuteNonQuery
级别显示以下错误:
“未处理OleDbException,标准表达式中的数据类型不匹配。”
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Src_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Src.Click
Dim Connection1 As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Database2.accdb;" & "Persist Security Info=False;" & "Jet OLEDB:Database Password=" & "your pass" & ";")
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Try
Connection1.Open()
cmd = New OleDbCommand("SELECT * from Table1 WHERE ID=" & IDtxt.Text & "", Connection1)
dr = cmd.ExecuteReader
If dr.Read Then
Me.IDtxt.Text = dr("ID")
Me.Hbtxt.Text = dr("Hb")
Me.TCtxt.Text = dr("TC")
Me.DCtxt.Text = dr("DC")
dr.Close()
Else
MsgBox("No Record")
End If
Catch
End Try
Connection1.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Connection1 As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Database2.accdb;")
Connection1.Open()
Dim ID : ID = Me.IDtxt.Text
Dim Hb : Hb = Me.Hbtxt.Text
Dim TC : TC = Me.TCtxt.Text
Dim DC : DC = Me.DCtxt.Text
Dim command As New OleDbCommand("UPDATE Table1 SET Hb = '" & Hb & "',TC = '" & TC & "',DC = '" & DC & "' WHERE ID = '" & ID & "'", Connection1)
Command.ExecuteNonQuery()
Connection1.Close()
End Sub
答案 0 :(得分:0)
在你的代码中使用参数......就像这个..
Dim command As New OleDbCommand("UPDATE Table1 SET Hb = @hb,TC = @tc,DC = @DC WHERE ID = @id", Connection1)
Command.parameters.add(new sqlparameter("@hb",Hb))
Command.parameters.add(new sqlparameter("@tc",tc))
Command.parameters.add(new sqlparameter("@dc",dc))
Command.parameters.add(new sqlparameter("@id",id))
Command.ExecuteNonQuery()