将结果存储在数组列表中
Posted
技术标签:
【中文标题】将结果存储在数组列表中【英文标题】:Storing results in array list 【发布时间】:2016-08-07 13:10:20 【问题描述】:我正在从 access 数据库返回值,但我想将结果中的一个值存储在数组列表中。
这样我就可以引入一个按钮,它运行相同的选择查询,但包含一个参数,该参数指出“客户 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】:要检查客户 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
【讨论】:
以上是关于将结果存储在数组列表中的主要内容,如果未能解决你的问题,请参考以下文章