使用 oledbdatareader C# 移动到下一条记录
Posted
技术标签:
【中文标题】使用 oledbdatareader C# 移动到下一条记录【英文标题】:Move to next record using oledbdatareader C# 【发布时间】:2016-07-21 17:59:44 【问题描述】:我创建了一种方法,可以使用 oledbdatareader 搜索访问数据库,但是我似乎无法弄清楚如何使用按钮移动到下一条记录。
请帮忙!
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 Surname = '" + textBox12.Text + "' OR Initial = '" + textBox12.Text + "' OR[Post Town] = '" + textBox12.Text + "' OR[Post Code] = '" + textBox12 + "'", conn);
command.Parameters.Add(new OleDbParameter("@Name", textBox12));
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
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();
// Insert code to process data.
finally
conn.Close();
【问题讨论】:
【参考方案1】:当您调用reader.Read()
时,您正在移动到下一条记录,建议加载您需要的数据,而不是根据用户输入从数据库中读取下一条记录。
你就是这样做的
应该首先定义一个包含所有字段的 DtoObject
public class DtoObject
public string FirstName get; set;
public string LastName get; set;
public string Address1 get; set;
public string Address2 get; set;
public string Address3 get; set;
public string TownCity get; set;
public string PostCode get; set;
public string Telephone get; set;
public string LstSvcDat get; set;
public string BoilerMan get; set;
public string BoilerMod get; set;
然后在您的方法中添加一个 Dto 列表,以及将它们加载到有效表单输入的方法,以及向前和向后移动项目的方法
//in your class
private readonly List<DtoObject> _ls = new List<DtoObject>();
private int _currentIndex = 0;
private void LoadDto(DtoObject object)
FirstName.Text = reader["Initial"].ToString();
LastName.Text = reader["Surname"].ToString();
...
BoilerMod.Text = reader["Model"].ToString();
private void MoveToNextItem()
_currentIndex++;
LoadDtoObject(_ls[_currentIndex]);
private void MoveToPreviousItem()
_currentIndex--;
LoadDtoObject(_ls[_currentIndex]);
然后在你的读取方法中将它们全部读入列表并将值初始化为第一个索引
//clear existing data
_ls.Clear();
while (reader.Read())
var current = new DtoObject();
current.FirstName = reader["Initial"].ToString();
current.LastName = reader["Surname"].ToString();
current.Address1= reader["Address 1"].ToString();
current.Address2= reader["Address 2"].ToString();
current.Address3= reader["Address 3"].ToString();
current.TownCity= reader["Post Town"].ToString();
current.PostCode= reader["Post Code"].ToString();
current.Telephone= reader["Telephone"].ToString();
current.LstSvcDat= reader["LastService"].ToString();
current.BoilerMan= reader["Manufacturer"].ToString();
current.BoilerMod= reader["Model"].ToString();
_ls.Add(current);
//if there are any set the first values to current form
if(_ls.Any())
_currentIndex = 0;
LoadDto(_ls.First());
else
//do something to notify no records were found
然后创建您的事件处理程序
private void nextItemButton_Click(object sender, EventArgs args)
MoveToNextItem();
private void prevItemButton_Click(object sender, EventArgs args)
MoveToPreviousItem();
【讨论】:
【参考方案2】:您需要将除reader.Read()
之外的所有内容移出点击处理程序。然后,第一次单击按钮(或在启动或加载时),打开阅读器。当有人点击按钮时,你Read
记录在案。
现在,您正在打开阅读器,阅读所有数据,然后在每次有人单击按钮时关闭阅读器。
【讨论】:
以上是关于使用 oledbdatareader C# 移动到下一条记录的主要内容,如果未能解决你的问题,请参考以下文章
C#通过OleDBDataReader从大型excel文件批量复制抛出内存异常
如何在 VB.NET 中使用 OleDbDataReader 搜索数据?