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

时间:2016-07-21 15:40:41

标签: c# ms-access

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

我的搜索查询正常运行但是当我运行更新查询时,搜索时仍会显示该记录。

有人可以帮忙吗?

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)
    {

    }
    }
}

1 个答案:

答案 0 :(得分:1)

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

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