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

时间:2017-12-24 09:52:40

标签: c# ms-access

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

我已将fullname和phone1设置为必需,并将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作为全名而programmer作为作业而2137976作为phone1号时,将抛出OleDbException并附带phone1的验证消息

  

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

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

1 个答案:

答案 0 :(得分:1)

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

如果我在MS Access中将验证规则更改为Like "[0-9]%",我可以插入带有电话号码的行&#34; 123&#34;通过在tableadapter上调用insert。

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

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

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

相关问题