使用oledbdatareader C#移至下一条记录

时间:2016-07-21 17:59:45

标签: c# ms-access oledb datareader

我创建了一个方法,我可以使用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();
        }

2 个答案:

答案 0 :(得分: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;} 
}

然后在你的方法中添加一个Dtos列表,一个将它们加载到有效表单输入的方法,以及向前和向后移动项目的方法

//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();
}

答案 1 :(得分:0)

您需要将除reader.Read()之外的所有内容移出点击处理程序。然后,第一次单击该按钮(或在启动或加载时),打开阅读器。当有人点击该按钮时,您{1}}会有记录。

现在,您正在打开阅读器,阅读所有数据,然后在每次有人点击按钮时关闭阅读器。