在Access数据库C#中归档客户

本文关键字:客户 Access 数据库 | 更新日期: 2023-09-27 17:58:01

我正在尝试设置一个方法,该方法允许我存档通过搜索框返回的记录。

我已经使搜索查询工作,但是当我运行更新查询时,搜索记录时仍然会出现。

有人能帮忙吗?

namespace Test_Application
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
        MainMenu MMform = new MainMenu();
        MMform.Show();
    }
    private void textBox3_TextChanged(object sender, EventArgs e)
    {
    }
    private void button2_Click(object sender, EventArgs e)
    {
        System.Data.OleDb.OleDbConnection conn = new
        System.Data.OleDb.OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb";
        try
        {
            conn.Open();
            OleDbCommand command = new OleDbCommand("SELECT Equipment.CustID AS CustID,Equipment.Manufacturer AS Manufacturer,Equipment.Model AS Model, Equipment.LastService AS LastService,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone FROM Contacts INNER JOIN Equipment ON Equipment.CustID = Contacts.CustID WHERE Contacts.Archived = 0 AND Surname = '" + textBox3.Text + "' OR Initial = '" + textBox3.Text + "' OR[Post Town] = '" + textBox3.Text + "' OR[Post Code] = '" + textBox3 + "'", conn);
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                int result;
                customid.Text = reader["CustID"].ToString();
                FirstName.Text = reader["Initial"].ToString();
                LastName.Text = reader["Surname"].ToString();
                Address1.Text = reader["Address 1"].ToString();
                Address2.Text = reader["Address 2"].ToString();
                Address3.Text = reader["Address 3"].ToString();
                TownCity.Text = reader["Post Town"].ToString();
                PostCode.Text = reader["Post Code"].ToString();
                Telephone.Text = reader["Telephone"].ToString();
                LstSvcDat.Text = reader["LastService"].ToString();
                BoilMan.Text = reader["Manufacturer"].ToString();
                BoilMod.Text = reader["Model"].ToString();
                result = Convert.ToInt32(customid.Text);
            }
         }
         finally
         {
             conn.Close();
         }
    }
    private void button3_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(LastName.Text))
        {
            MessageBox.Show("Please Search for a Customer First");
        }
        else
        {
            System.Data.OleDb.OleDbConnection conn = new
            System.Data.OleDb.OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb";
            try
            {
                conn.Open();
                OleDbCommand command = new OleDbCommand("UPDATE Contacts SET Archived = 1 WHERE CustID = @CustID", conn);
                command.Parameters.Add(new OleDbParameter("@PCustID", customid.Text));
                OleDbDataReader reader = command.ExecuteReader();
            }
            finally
            {
                conn.Close();
                customid.Text = null;
                FirstName.Text = null;
                LastName.Text = null;
                Address1.Text = null;
                Address2.Text = null;
                Address3.Text = null;
                TownCity.Text = null;
                PostCode.Text = null;
                Telephone.Text = null;
                LstSvcDat.Text = null;
                BoilMan.Text = null;
                BoilMod.Text = null;
                MessageBox.Show("Customer Archived");
            }
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
    }
}

在Access数据库C#中归档客户

我怀疑错误实际上在您的搜索查询中。当省略括号时,AND和OR的顺序被分组。尝试:

WHERE Contacts.Archived = 0 AND ((Surname = '" + textBox3.Text + "' OR Initial = '" + textBox3.Text + "') OR ([Post Town] = '" + textBox3.Text + "' OR[Post Code] = '" + textBox3 + "'))"