我目前正在创建存储停车信息的数据库。现在我能够成功连接到数据库并添加一个条目,然后添加另一个条目等。但是,当我在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
答案 0 :(得分:3)
以下是我可以立即看到的一些问题
Open
com
初始化为命令,但从不使用它。没有配置参数com1
和com2
设置其参数,但从不执行其中任何一个com
的行已被注释掉。您应初始化com
,设置其参数,打开连接,然后致电ExecuteNonQuery
并撤消设置com1
和com2
的所有代码 - 两者均为无关紧要和不必要的。
例如:
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();
}
}
顺便说一句,您不应该对连接字符串进行硬编码,而是从配置文件中读取它。
答案 1 :(得分:2)
您的命令已设置,但您永远不会执行查询。执行是您实际运行查询命令并将记录保存在数据库中所需的。您需要根据需要致电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 = "";
}
}