我是视觉基础的新手。我想根据电话号码删除记录。程序将要求用户输入他的号码,并在将电话号码与数据库匹配后删除记录。我试过这样做,但是它给了我一个错误"在标准表达式中键入不匹配"。 我正在使用ms access 2007并且我已经将电话列的数据类型设置为Number。这是我的vb代码。请帮我 。这一定是我愚蠢的错误之一。在插入数据模块中,我已将数据类型指定为Long,在此之前我已将其指定为Integer但仍存在相同的错误。
Imports System.Data.OleDb
Public Class Form5
Public Connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\AMEN\Documents\Railway.accdb"
Public Conn As New OleDbConnection
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Conn.ConnectionString = Connstring
If Conn.State = ConnectionState.Closed Then
Conn.Open()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim okToDelete As MsgBoxResult = MsgBox("Are you sure you want to cancel your ticket?", MsgBoxStyle.YesNo)
If okToDelete = MsgBoxResult.Yes Then
Dim str As String
str = "Delete from Table2 Where Phone = '" & TextBox1.Text & "'"
Dim cmd As OleDbCommand = New OleDbCommand(str, Conn)
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
Conn.Close()
MsgBox("Your Ticket Cancelled Sucessfully ! ")
Catch ex As Exception
MsgBox(ex.Message)
End Try
ElseIf okToDelete = MsgBoxResult.No Then
End If
End Sub
End Class
答案 0 :(得分:0)
电话号码应存储为字符串而不是数字。
但是,如果您的电话字段是数字,那么您的查询中的一部分是错误的
在这种情况下,TextBox1.Text
周围不需要引号。这会将您的输入视为字符串,并且此字符串不能用于在数字字段中搜索。
最好的方法是使用参数化查询,您可以在其中指定传递的参数的数据类型 (我假设你已经在Access中创建了电话号码作为长号号码)
If okToDelete = MsgBoxResult.Yes Then
' Check if your user types really a number.
Dim number As Integer
if Not Int32.TryParse(TextBox1.Text, number) Then
MessageBox.Show("Not a valid number!")
return
End If
Dim str As String
str = "Delete from Table2 Where Phone = @phone"
Dim cmd As OleDbCommand = New OleDbCommand(str, Conn)
Try
cmd.Parameters.Add("@phone", OleDbType.Integer).Value = number
cmd.ExecuteNonQuery()
....