2014-01-29 32 views
1

我在我的数据库中有一个名为学生的表,有数字和名称,地址....
我有一个表单,我一次加载一个学生的所有信息,我有一个下一个按钮和一个后退按钮。
如何迭代到mysql中的下一行(或前一行)(以便能够查看下一个学生的信息)?
我试图使用主键(自动增量)迭代,当我想看到下一个记录我加1到ID或减1以查看以前的记录。
但是,如果一条记录被删除,它将显示一个空记录。 你能指出我的方向吗?
使用I'm的WinForms
对不起我的英语..迭代trouthth数据库表行C#

  string config = "server=localhost; userid = root; database = databaseName"; 
      MySqlConnection con = new MySqlConnection(config); 

      MySqlDataReader reader = null; 
      string query = "SELECT * FROM students WHERE id = " + id; //id is the primary Key (auto increment) 

      MySqlCommand command = new MySqlCommand(query, con); 
      con.Open(); 
      reader = command.ExecuteReader(); 

      while (reader.Read()) 
      { 

       string studentName = (string)reader["studentName"]; 

       string studentNum = (string)reader["studentNum"]; 

       tbstudentName.Text = Convert.ToString(studentName); 
       tbstudentNum.Text = Convert.ToString(studentNum);     

       ..... 
      } 
      con.Close(); 

回答

0

你不应该叫您要查看下一条记录每次数据库。尝试将所有数据读入列表。

我不确定你在用什么.. WinForms? WPF?

如果WinForms你需要做这样的事情。

public class Student 
    {//First create a class to hold your data in 
     public string Name { get; set; } 
     public string Num { get; set; } 
    } 

public class MyForm : Form 
{ 
    int Index = 0; 
    List<Student> FormData { get; set; } 

    void GetData() 
    { 
    //This will hold all your data in memory so you do not have to make a database call each and every "iteration" 
    List<Student> dbData = new List<Student>(); 

    string config = "server=localhost; userid = root; database = databaseName"; 
     MySqlConnection con = new MySqlConnection(config); 

     MySqlDataReader reader = null; 
     string query = "SELECT * FROM students"; 

     MySqlCommand command = new MySqlCommand(query, con); 
     con.Open(); 
     reader = command.ExecuteReader(); 

     while (reader.Read()) 
     { 
      Student newStudent = new Student(); 

      newStudent.Name = (string)reader["studentName"]; 

      newStudent.Num = (string)reader["studentNum"]; 
      //Add data to the list you created 
      dbData.Add(newStudent);   

      ..... 
     } 
     con.Close(); 

     //set the Form's list equal to the one you just populated 
     this.FormData = dbData; 
    } 

    private void BindData() 
    { 
     //If winforms 
     tbstudentName.Text = FormData[Index].Name; 
     tbstudentNum.Text = FormData[Index].Num; 

     //If wpf you will have to use view models and bind your data in your XAML but I am assuming you are using 
     //winforms here. 
    } 

    private void NextRecord() 
    { //If you reached the end of the records then this will prevent IndexOutOfRange Exception 
     if (Index < FormData.Count - 1) 
     { 
      Index++; 
      BindData(); 
     } 
    } 

    private void PreviousRecord() 
    { 
     if (Index != 0) 
     { 
      Index--; 
      BindData(); 
     } 
    } 
} 

现在上面的场景会使它快速工作;然而,当你需要改变数据时,有更好的方法可以帮助你。我会推荐WinForms绑定。你可以看看这里http://msdn.microsoft.com/en-us/library/c8aebh9k(v=vs.110).aspx

+0

我试过你的建议,但我得到这一行中的错误if(Index

+0

你能更新你的答案与所有​​你使用的代码?这可能意味着你的'FormData'为空,并且从未设置过。我会为你检查一下。 – Adrian

+0

我当时很蠢......问题解决了。谢谢 –

0

DataReader旨在快速读取一次。

如果你想保存数据,你需要填充内存数组。 DataTable很好地实现它。

0

您需要考虑一点点不同。

得到id+1你正在非常不小心..即使是身份证,身份证可以是另一个值,你会得到一个例外..我想你不想要它。

您需要调整您的逻辑与top回线,或在MySQL中,limit声明..

这将使用lambda使用.Take()Skip()方法...... 您也可以使用容易限制参数来传递通过量这个样本..你可以理解..

MySQL skip first 10 results

希望它能帮助。

1

为了让你可以写下一个:

select * from students where id > @id 
order by id asc 
limit 1 

而且得到以前

select * from students where id < @id 
order by id desc 
limit 1