为什么当我在应用程序中单击保存时会出现"Microsoft JET Database Engine"弹出窗口?



我正在创建一个应用程序将条目添加到数据库中,但是当我单击保存时,我得到一个弹出窗口,上面写着"Microsoft JET数据库引擎"而没有其他内容。现在我对编程相当陌生,所以我不知道这是什么意思。我试着查找它,但我发现的是各种错误代码与JET数据库引擎在他们。

我使用c#创建应用程序和数据库是使用Access 2013创建的。

下面是部分代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
using System.Data;
using System.ComponentModel;

namespace ParkingDatabase
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    OleDbConnection Connect = new OleDbConnection();
    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
        Connect.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:UsersbkosoDocumentsVisual Studio 2015ProjectsParkingDatabaseParkingDatabaseParkingData.mdb";
        Connect.Open();
        OleDbCommand com = new OleDbCommand("Insert Into Guest Info(Guest First Name, Guest Last Name, Room Number, Departure Date, Return Date, Vehicle Colour, Vehicle Make, License Plate Number, Contact First Name, Contact Last Name, Contact Phone Number, Contact Email, Park And Fly Tag Number) Values(@Guest First Name, @Guest Last Name, @Room Number, @Departure Date, @Return Date, @Vehicle Colour, @Vehicle Make, @License PlateNumber, @Contact First Name, @Contact Last Name, @Contact Phone Number, @Contact Email, @Park And Fly Tag Number)", Connect);
            com.Parameters.AddWithValue("@Guest First Name", txtBxGstFName.Text);
            com.Parameters.AddWithValue("@Guest Last Name", txtBxGstLName.Text);
            com.Parameters.AddWithValue("@Room Number", txtBxRm.Text);
            com.Parameters.AddWithValue("@Departure Date", txtBxDDate.Text);
            com.Parameters.AddWithValue("@Return Date", txtBxRDate.Text);
            com.Parameters.AddWithValue("@Vehicle Colour", txtBxVColour.Text);
            com.Parameters.AddWithValue("@Vehicle Make", txtBxVMake.Text);
            com.Parameters.AddWithValue("@License Plate Number", txtBxPlate.Text);
            com.Parameters.AddWithValue("@Contact First Name", txtBxContactFName.Text);
            com.Parameters.AddWithValue("@Contact Last Name", txtBxContactLName.Text);
            com.Parameters.AddWithValue("@Contact Phone Number", txtBxPhone.Text);
            com.Parameters.AddWithValue("@Contact Email", txtBxEmail.Text);
            com.Parameters.AddWithValue("@Park And Fly Tag Number", txtBxTag.Text);
        if (Connect.State == ConnectionState.Open)
        {
            try
            {
                com.ExecuteNonQuery();
                MessageBox.Show("Guest Information Saved Successfully");
                txtBxGstFName.Text = "";
                txtBxGstLName.Text = "";
                txtBxRm.Text = "";
                txtBxDDate.Text = "";
                txtBxRDate.Text = "";
                txtBxVColour.Text = "";
                txtBxVMake.Text = "";
                txtBxPlate.Text = "";
                txtBxContactFName.Text = "";
                txtBxContactLName.Text = "";
                txtBxPhone.Text = "";
                txtBxEmail.Text = "";
                txtBxTag.Text = "";
            }
            catch (Exception notSaved)
            {
                MessageBox.Show(notSaved.Source);
                Connect.Close();
            }
        }
        else
        {
            MessageBox.Show("Connection Failed");
        }
    }

谢谢

您可能在btnSave_Click方法中遇到错误。

try... catch部分的catch块显示错误的Source消息框,而不是错误的Message

catch (Exception notSaved)
{
    MessageBox.Show(notSaved.Source);
    Connect.Close();
}

按如下方式更改此部分,以便您可以向用户显示实际的错误消息。

另外,只有当连接不为空且尚未关闭时才尝试关闭连接。否则你会在异常中得到一个异常:)

catch (Exception notSaved)
{
    if (Connect != null && Connect.State != ConnectionState.Closed)
    {
         Connect.Close();
    }
    MessageBox.Show("Error saving data n" + notSaved.Message);
}

最新更新