我需要在Visual Studio上使用C#进行Microsoft Access数据库分配的帮助。我已经编写了所有代码,但是在组合框事件处理程序中,查询消息区域始终出现错误。我不断收到的错误消息说我在邮政编码表和INNER JOIN区域的查询表达式有问题。
这是代码-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Final_Programming
{
public partial class customerInformationFRM : Form
{
public customerInformationFRM()
{
InitializeComponent();
}
OleDbConnection cusInfoConnection = new OleDbConnection();
string chosenCustomer;
// Method for access database connection
private void databaseConnection()
{
try
{
// Establishes where provider to go to in order to open microsoft access file
string connect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
"FinalDatabase.accdb;Persist Security Info=False;";
// Open database connection
cusInfoConnection.ConnectionString = connect;
cusInfoConnection.Open();
}
catch (Exception errMsg)
{
// Messagebox pops up if theres an error
MessageBox.Show("Error in databaseConnection method: " + errMsg.Message,
"databaseConnection method error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
// Method to fill combo box
private void fillComboBox()
{
try
{
string cusInfoQRY = "SELECT PersonalInfo.[IDnumber] FROM PersonalInfo";
// Define Adapter
OleDbDataAdapter cusNumDA = new OleDbDataAdapter(cusInfoQRY, cusInfoConnection);
cusNumDA.Fill(cusNumDS, "IDnumber");
cusNumCMB.DataSource = cusNumDS.Tables[0];
cusNumCMB.DisplayMember = "IDnumber";
cusNumCMB.ValueMember = "IDnumber";
}
catch (Exception errMsg)
{
MessageBox.Show("Error in fill combo box method: " + errMsg.Message,
"Combo box error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void customerInformationFRM_Load(object sender, EventArgs e)
{
try
{
databaseConnection();
fillComboBox();
}
catch (Exception errMsg)
{
MessageBox.Show("Error in form load: " + errMsg.Message,
"Form load error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void cusNumCMB_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cusNumDS.Clear();
customerInfoDS.Clear();
orderInfoDS.Clear();
chosenCustomer = cusNumCMB.Text;
if (chosenCustomer != "System.Data.DataRowView")
{
string cusInfoSQL = "SELECT PersonalInfo.FirstName, PersonalInfo.LastName, PersonalInfo.PhoneNumber, " +
"PersonalInfo.EmailAddress, PersonalInfo.Address, ZipCode.City, ZipCode.State, ZipCode.Zip" +
"FROM ZipCode INNER JOIN PersonalInfo ON ZipCode.[Zip] = PersonalInfo.[Zip] where PersonalInfo.IDnumber = '" +
chosenCustomer + "'";
OleDbDataAdapter customerNumberDA = new OleDbDataAdapter(cusInfoSQL, cusInfoConnection);
customerNumberDA.Fill(customerInfoDS, "customerInfo");
DataRow customerInfoDR = customerInfoDS.Tables[1].Rows[0];
firstNameTB.Text = customerInfoDR[0].ToString();
lastNameTB.Text = customerInfoDR[1].ToString();
phoneNumTB.Text = customerInfoDR[2].ToString();
emailTB.Text = customerInfoDR[3].ToString();
addressTB.Text = customerInfoDR[4].ToString();
cityTB.Text = customerInfoDR[5].ToString();
stateTB.Text = customerInfoDR[6].ToString();
zipTB.Text = customerInfoDR[7].ToString();
string orderSQL = "SELECT OrderInfo.[OrderDate], " +
"OrderInfo.[OrderShipped], OrderInfo.[ShippingFee]FROM OrderInfo where " +
"OrderInfo.[OrderNumber] = '" + chosenCustomer + "'";
OleDbDataAdapter itemsDA = new OleDbDataAdapter(orderSQL, cusInfoConnection);
itemsDA.Fill(orderInfoDS, "order");
decimal total = 0.0m;
foreach (DataRow currentRow in orderInfoDS.Tables[0].Rows)
{
total = Convert.ToDecimal(currentRow[3]);
customerDataDGV.Rows.Add(currentRow[0].ToString(), currentRow[1].ToString(), currentRow[2].ToString(), Convert.ToString(total.ToString("C")));
totalShippingTB.Text = Convert.ToString(total.ToString("C"));
}
}
}
catch (Exception errMsg)
{
MessageBox.Show("Error in combo box event handler: " + errMsg.Message,
"Combo box error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}