2014-01-29 30 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