将结果存储在数组列表中

时间:2016-08-07 13:10:20

标签: c# oledb datareader

我从访问数据库返回值,但我想将结果中的一个值存储在数组列表中。

这样我就可以引入一个按钮,它运行相同的选择查询,但包含一个参数,指出'客户ID不等于存储在数组列表中的值'

有人可以帮忙吗?

    public void LoopThroughRecs(OleDbDataReader Reader)
    {

        if (Reader.Read())
        {
            int result;

            CustID.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();
            BoilerMan.Text = Reader["Manufacturer"].ToString();
            BoilerMod.Text = Reader["Model"].ToString();

            result = Convert.ToInt32(CustID.Text);

        }
    }

    public void SearchButton_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 Contacts.CustID AS CustID,CustCode,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Equipment.CustID AS CustID1,Equipment.Manufacturer AS Manufacturer,Equipment.Model AS Model,Equipment.LastService AS LastService FROM Contacts LEFT OUTER JOIN Equipment ON Equipment.CustID = Contacts.CustID WHERE Archived = 0 AND ([Address 1] = '" + textBox12.Text + "' OR CustCode LIKE '" + textBox12.Text + '%' + "' OR Initial = '" + textBox12.Text + '%' + "' OR Surname = '" + textBox12.Text + '%' + "' OR Initial = '" + textBox12.Text + '%' + "' OR [Post Town] LIKE '" + textBox12.Text + '%' + "' OR [Post Code] = '" + textBox12 + '%' + "')", conn);
            OleDbDataReader Reader = command.ExecuteReader();

            LoopThroughRecs(Reader);

        }
        finally
        {
            conn.Close();
        }
    }

1 个答案:

答案 0 :(得分:0)

要检查客户ID不等于存储在数组列表中的值,可以使用以下方法:

 public bool ItemIsExist(ArrayList al, int id)
    {
        var NotInArryList = al.ToArray().Where(x => !al.Contains(id));
        var flag = NotInArryList.Count() == 0;
        Console.WriteLine("{0}  {1}", id, flag ? " Is Exist" : "Not Exist");
        return flag;
    }

<强>用法:

var isExist =  ItemIsExist(your_Array_List, Customer.ID)

示例:

        var id = 1;
        ArrayList al = new ArrayList();
        al.Add(id);
        Console.WriteLine("add 2 to the ArrayList");
        ItemIsExist(al, 2);
        al.Add((2));
        ItemIsExist(al, 2);

<强>结果:

2  Not Exist
add 2 to the ArrayList
2   Is Exist