对于访问接受的输入,抛出oledbexception

问题描述 投票:1回答:1

我正在构建一个小型电话簿应用程序,我将电话存储在名为phone的表中的访问数据库中,其字段为:FullName,Job,Phone1,Phone2,Notes和SN主键。

我已将fullname和phone1设置为required并将fullname的验证规则设置为Len([FullName])>3,并将phone1和phone2的验证规则设置为Like "[0-9]*",我还为验证规则设置了验证消息。

在wpf应用程序中,我使用visual studio添加了访问数据库来生成数据集和tableadapter代码,这是MainWindow.xaml:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="253" Width="685">
<Grid Height="206" Width="658">
    <Label Content="FullName" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Label1" VerticalAlignment="Top" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="78,16,0,0" Name="FullName" VerticalAlignment="Top" Width="185" />
    <Label Content="Job" Height="28" HorizontalAlignment="Left" Margin="29,46,0,0" Name="Label2" VerticalAlignment="Top" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="78,46,0,0" Name="Job" VerticalAlignment="Top" Width="185" />
    <Label Content="Phone1" Height="28" HorizontalAlignment="Left" Margin="22,75,0,0" Name="Label3" VerticalAlignment="Top" />
    <Label Content="Phone2" Height="28" HorizontalAlignment="Left" Margin="269,16,0,0" Name="Label4" VerticalAlignment="Top" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="78,75,0,0" Name="Phone1" VerticalAlignment="Top" Width="110" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="325,21,0,0" Name="Phone2" VerticalAlignment="Top" Width="110" />
    <Label Content="Notes" Height="28" HorizontalAlignment="Left" Margin="274,56,0,0" Name="Label5" VerticalAlignment="Top" />
    <TextBox Height="95" HorizontalAlignment="Left" Margin="325,60,0,0" Name="Notes" VerticalAlignment="Top" Width="328" AcceptsReturn="True" AcceptsTab="True" />
    <Button Content="Save" Height="37" HorizontalAlignment="Left" Margin="274,161,0,0" Name="Button2" VerticalAlignment="Top" Width="135" />
</Grid>

这是保存点击处理程序:

private button2_Click(object sender , RoutedEventArgs e)
{
    try
    {
        PhonesDataSetPhonesTableAdapter.Insert(FullName.Text, Job.Text, Phone1.Text, Phone2.Text, Notes.Text);
    } catch(OleDbException ex) {
        MessageBox.Show(ex.Message);
    }        
}

PhonesDataSetPhonesTableAdapter定义如下:

Global.projectName.PhonesDataSetTableAdapters.PhonesTableAdapter PhonesDataSetPhonesTableAdapter = new Global.projectName.PhonesDataSetTableAdapters.PhonesTableAdapter();

当我启动应用程序并将John作为fullname并将programmer作为job而2137976作为phone1号时,将抛出OleDbException,其中包含phone1的验证消息,即

您的电话号码包含字母,它应该只包含数字

但它不包含任何字母,当我通过访问尝试相同的输入时,它会接受它,这里发生了什么?以及如何使其发挥作用。

c# ms-access
1个回答
1
投票

OleDb驱动程序在其Like语句中需要不同的通配符。而不是*它现在想要一个%(这是ANSI-92)。

如果我在MS Access中将验证规则更改为Like "[0-9]%",我可以通过在该tableadapter上调用insert来插入带有“123”的行。

这样做的缺点是在Access中你不能再插入值,因为Access现在需要一个数字后的文字字符%。

如果您希望应用程序和访问权限都能正常工作,那么使用Odbc驱动程序将会起作用。

以下是该问题的一些背景:Microsoft Jet wildcards: asterisk or percentage sign?

© www.soinside.com 2019 - 2024. All rights reserved.