使用c#获取访问数据库中所有条目的Null值.为什么

本文关键字:Null 为什么 获取 访问 数据库 使用 | 更新日期: 2023-09-27 18:12:54

我目前正在创建一个存储停车信息的数据库。现在我能够成功地连接到数据库并添加一个条目,然后添加另一个条目等。但是,当我在Access中查看表时,我看不到我在每个记录中输入的任何内容,当我在Visual Studio中查看表时,它显示每个记录为Null。

我做错了什么?我不仅对编程很陌生,而且对数据库也很陌生。

下面是我的应用程序的代码:
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();
    }

    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
        using (OleDbConnection DBConnect = new OleDbConnection())
        {
            DBConnect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:'Users'bkoso'documents'visual studio 2015'Projects'ParkingDatabase'ParkingDatabase'ParkingData.accdb";
            OleDbCommand com = new OleDbCommand();
            if (DBConnect.State == ConnectionState.Open)
            {
                OleDbCommand com1 = new OleDbCommand("INSERT INTO [Guest Info]([Guest First Name], [Guest Last Name], [Room Number], [Departure Date], [Return Date], [Vehicle Colour], [Vehicle Make], [Plate Number], [Contact First Name], [Contact Last Name], [Contact Number], [Contact Email], [Tag Number]) Values(@[Guest First Name], @[Guest Last Name], @[Room Number], @[Departure Date], @[Return Date], @[Vehicle Colour], @[Vehicle Make], @[Plate Number], @[Contact First Name], @[Contact Last Name], @[Contact Email], @[Contact Email], @[Tag Number])", DBConnect);
                com1.Parameters.AddWithValue("@[Guest First Name]", txtBxGstFName.Text);
                com1.Parameters.AddWithValue("@[Guest Last Name]", txtBxGstLName.Text);
                com1.Parameters.AddWithValue("@[Room Number]", txtBxRm.Text);
                com1.Parameters.AddWithValue("@[Departure Date]", txtBxDDate.Text);
                com1.Parameters.AddWithValue("@[Return Date]", txtBxRDate.Text);
                com1.Parameters.AddWithValue("@[Vehicle Colour]", txtBxVColour.Text);
                com1.Parameters.AddWithValue("@[Vehicle Make]", txtBxVMake.Text);
                com1.Parameters.AddWithValue("@[Plate Number]", txtBxPlate.Text);
                com1.Parameters.AddWithValue("@[Contact First Name]", txtBxContactFName.Text);
                com1.Parameters.AddWithValue("@[Contact Last Name]", txtBxContactLName.Text);
                com1.Parameters.AddWithValue("@[Contact Number]", txtBxPhone.Text);
                com1.Parameters.AddWithValue("@[Contact Email]", txtBxEmail.Text);
                com1.Parameters.AddWithValue("@[Tag Number]", txtBxTag.Text);
            }
            else
            {
                DBConnect.Open();
                OleDbCommand com2 = new OleDbCommand("INSERT INTO [Guest Info]([Guest First Name], [Guest Last Name], [Room Number], [Departure Date], [Return Date], [Vehicle Colour], [Vehicle Make], [Plate Number], [Contact First Name], [Contact Last Name], [Contact Number], [Contact Email], [Tag Number]) Values(@[Guest First Name], @[Guest Last Name], @[Room Number], @[Departure Date], @[Return Date], @[Vehicle Colour], @[Vehicle Make], @[Plate Number], @[Contact First Name], @[Contact Last Name], @[Contact Email], @[Contact Email], @[Tag Number])", DBConnect);
                com2.Parameters.AddWithValue("@[Guest First Name]", txtBxGstFName.Text);
                com2.Parameters.AddWithValue("@[Guest Last Name]", txtBxGstLName.Text);
                com2.Parameters.AddWithValue("@[Room Number]", txtBxRm.Text);
                com2.Parameters.AddWithValue("@[Departure Date]", txtBxDDate.Text);
                com2.Parameters.AddWithValue("@[Return Date]", txtBxRDate.Text);
                com2.Parameters.AddWithValue("@[Vehicle Colour]", txtBxVColour.Text);
                com2.Parameters.AddWithValue("@[Vehicle Make]", txtBxVMake.Text);
                com2.Parameters.AddWithValue("@[Plate Number]", txtBxPlate.Text);
                com2.Parameters.AddWithValue("@[Contact First Name]", txtBxContactFName.Text);
                com2.Parameters.AddWithValue("@[Contact Last Name]", txtBxContactLName.Text);
                com2.Parameters.AddWithValue("@[Contact Number]", txtBxPhone.Text);
                com2.Parameters.AddWithValue("@[Contact Email]", txtBxEmail.Text);
                com2.Parameters.AddWithValue("@[Tag Number]", txtBxTag.Text);
            }

            if (DBConnect.State == ConnectionState.Open)
            {
                //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 = "";
        }
    }    
    private void btnClear_Click(object sender, RoutedEventArgs e)
    {
        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 = "";
    }
    private void btnView_Click(object sender, RoutedEventArgs e)
    {
    }
    private void btnSame_Click(object sender, RoutedEventArgs e)
    {
    }
    private void txtBoxGuestFirstName_TextChanged(object sender, TextChangedEventArgs e)
    {
    }
    private void btnDelete_Click(object sender, RoutedEventArgs e)
    {
    }
    private void btnSearch_Click(object sender, RoutedEventArgs e)
    {
    }
}
}

任何帮助都将是非常感激的。

Bloodstalker

使用c#获取访问数据库中所有条目的Null值.为什么

这里有几个我可以直接看到的问题

  1. 你每次都创建一个新的连接-这是正确的-但它的状态永远不会是Open
  2. 您将com初始化为命令,但从不使用它。未配置参数
  3. 初始化com1com2,设置它们的参数,但从不执行它们中的任何一个
  4. 实际执行com的行被注释掉。

您应该初始化com,设置其参数,打开连接,调用ExecuteNonQuery,并丢弃设置com1com2的所有代码-两者都是不相关和不必要的。

例如:

using (OleDbConnection DBConnect = new OleDbConnection())
{
      DBConnect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:'Users'bkoso'documents'visual studio 2015'Projects'ParkingDatabase'ParkingDatabase'ParkingData.accdb";
     using(OleDbCommand com = new OleDbCommand("INSERT INTO [Guest Info]([Guest First Name], [Guest Last Name], [Room Number], [Departure Date], [Return Date], [Vehicle Colour], [Vehicle Make], [Plate Number], [Contact First Name], [Contact Last Name], [Contact Number], [Contact Email], [Tag Number]) Values(@[Guest First Name], @[Guest Last Name], @[Room Number], @[Departure Date], @[Return Date], @[Vehicle Colour], @[Vehicle Make], @[Plate Number], @[Contact First Name], @[Contact Last Name], @[Contact Email], @[Contact Email], @[Tag Number])", DBConnect))
     {
       com.Parameters.AddWithValue("@[Guest First Name]", txtBxGstFName.Text);
       com.Parameters.AddWithValue("@[Guest Last Name]", txtBxGstLName.Text);
       // .. snip other params .. //
       DBConnect.Open();
       com.ExecuteNonQuery();
       DBConnect.Close();
     }
}

顺便说一句,不应该硬编码连接字符串,而应该从配置文件中读取。

您的命令已经设置好了,但是您从未执行过查询。执行是实际运行查询命令并将记录保存到数据库中所需要的。您需要根据需要调用com1.ExecuteNonQuery()com2.ExecuteNonQuery()

你很可能把这个方法简化成如下的样子:

    private void btnSave_Click(object sender, RoutedEventArgs e)
{
    using (OleDbConnection DBConnect = new OleDbConnection())
    {
        DBConnect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:'Users'bkoso'documents'visual studio 2015'Projects'ParkingDatabase'ParkingDatabase'ParkingData.accdb";
        OleDbCommand com = new OleDbCommand("INSERT INTO [Guest Info]([Guest First Name], [Guest Last Name], [Room Number], [Departure Date], [Return Date], [Vehicle Colour], [Vehicle Make], [Plate Number], [Contact First Name], [Contact Last Name], [Contact Number], [Contact Email], [Tag Number]) Values(@[Guest First Name], @[Guest Last Name], @[Room Number], @[Departure Date], @[Return Date], @[Vehicle Colour], @[Vehicle Make], @[Plate Number], @[Contact First Name], @[Contact Last Name], @[Contact Email], @[Contact Email], @[Tag Number])", DBConnect);
        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("@[Plate Number]", txtBxPlate.Text);
        com.Parameters.AddWithValue("@[Contact First Name]", txtBxContactFName.Text);
        com.Parameters.AddWithValue("@[Contact Last Name]", txtBxContactLName.Text);
        com.Parameters.AddWithValue("@[Contact Number]", txtBxPhone.Text);
        com.Parameters.AddWithValue("@[Contact Email]", txtBxEmail.Text);
        com.Parameters.AddWithValue("@[Tag Number]", txtBxTag.Text);
        // don't forget to execute the query!
        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 = "";
    }
}