[C#] 檢索數據庫並用DataGridView 控件顯示數據.

原文鏈接:http://blog.csdn.net/shylx123/article/details/7935922


練習點:

數據庫讀取數據並保存在一個集合中.

使用DataGridView來顯示數據.


1  Form1 用於用戶登錄驗證


Form1 Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace DataGridViewTest2  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             string username = txtUsername.Text;  
  22.             string password = txtPassword.Text;  
  23.   
  24.             DBConnect dbc = new DBConnect();  
  25.   
  26.             int s = dbc.UserLogin(username, password);  
  27.   
  28.             if (1 == s)  
  29.             {  
  30.                 MessageBox.Show("Incorrect Username");  
  31.             }  
  32.             if (2 == s)  
  33.             {  
  34.                 MessageBox.Show("Incorrect Password");  
  35.             }  
  36.             if(0 == s)  
  37.             {  
  38.                 Form2 form2 = new Form2(this);  
  39.                 //this.Hide();  
  40.                 form2.ShowDialog();  
  41.             }  
  42.   
  43.         }  
  44.     }  
  45. }  

2. Form2, 用記顯示數據, 它只有一個DataGridView控件


Form2 Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Collections;  
  10.   
  11. namespace DataGridViewTest2  
  12. {  
  13.     public partial class Form2 : Form  
  14.     {  
  15.         Form1 form1;  
  16.         public Form2(Form1 form1)  
  17.         {  
  18.             InitializeComponent();  
  19.             this.form1 = form1;  
  20.         }  
  21.   
  22.          
  23.         private void Form2_Load(object sender, EventArgs e)  
  24.         {  
  25.             DBConnect dbc = new DBConnect();  
  26.             ArrayList UserList = new ArrayList();  
  27.   
  28.             UserList = dbc.ExportData();  
  29.             UserList.Reverse();  
  30.               
  31.             //DataGridView設置  
  32.             DataTable DataTable1 = new DataTable();  
  33.             DataTable1.Columns.AddRange(new DataColumn[] { new DataColumn("ID"), new DataColumn("Name"), new DataColumn("Sex"), new DataColumn("Age"), new DataColumn("Address") });  
  34.   
  35.             foreach (User users in UserList)  
  36.             {  
  37.                   
  38.                 DataTable1.Rows.Add(users.Id, users.Username, users.Sex==0?"男":"女" , users.Age, users.Address);  
  39.             }  
  40.              
  41.   
  42.             dataGridView1.DataSource = DataTable1;  
  43.         }  
  44.   
  45.           
  46.   
  47.     }  
  48. }  


3. BDConnect.cs, 數據庫鏈接, 用於驗證和提取數據

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data.SqlClient;  
  6. using System.Collections;  
  7.   
  8. namespace DataGridViewTest2  
  9. {  
  10.     class DBConnect  
  11.     {  
  12.         string username, password;  
  13.   
  14.         public int UserLogin(string username, string password)  
  15.         {  
  16.             this.username = username;  
  17.             this.password = password;  
  18.   
  19.             //下面這段文字的作用已經在其它博文中解釋!    
  20.             string dataDir = AppDomain.CurrentDomain.BaseDirectory;  
  21.             if (dataDir.EndsWith(@"\bin\Debug\")  
  22.                 || dataDir.EndsWith(@"\bin\Release\"))  
  23.             {  
  24.                 dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;  
  25.                 AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);  
  26.             }    
  27.   
  28.   
  29.             using(SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserInfo.mdf;Integrated Security=True;User Instance=True"))  
  30.             {  
  31.                 conn.Open();  
  32.   
  33.                 using(SqlCommand cmd = conn.CreateCommand())  
  34.                 {  
  35.                     cmd.CommandText = "SELECT * FROM UserLogin WHERE username='"+username + "'";  
  36.   
  37.                     using(SqlDataReader reader = cmd.ExecuteReader())  
  38.                     {  
  39.                         if(!reader.Read())  
  40.                         {  
  41.                             return 1;//沒有此用戶  
  42.                         }  
  43.                         else  
  44.                         {  
  45.                             string dbpassword = reader.GetString(reader.GetOrdinal("password"));  
  46.                             if(dbpassword != password)  
  47.                             {  
  48.                                 return 2;//用戶密碼不對  
  49.                             }  
  50.                             else  
  51.                             {  
  52.                                 return 0;  
  53.                             }  
  54.                         }  
  55.                     }  
  56.                 }  
  57.             }  
  58.         }  
  59.   
  60.   
  61.         public ArrayList ExportData()  
  62.         {  
  63.             ArrayList UserList = new ArrayList();  
  64.   
  65.             //下面這段文字的作用已經在其它博文中解釋!    
  66.             string dataDir = AppDomain.CurrentDomain.BaseDirectory;  
  67.             if (dataDir.EndsWith(@"\bin\Debug\")  
  68.                 || dataDir.EndsWith(@"\bin\Release\"))  
  69.             {  
  70.                 dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;  
  71.                 AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);  
  72.             }    
  73.   
  74.   
  75.             using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserInfo.mdf;Integrated Security=True;User Instance=True"))  
  76.             {  
  77.                 conn.Open();  
  78.   
  79.                 using (SqlCommand cmd = conn.CreateCommand())  
  80.                 {  
  81.                     cmd.CommandText = "SELECT * FROM UserInfo";  
  82.   
  83.                     using (SqlDataReader reader = cmd.ExecuteReader())  
  84.                     {  
  85.                         while (reader.Read())  
  86.                         {  
  87.                             User users = new User();  
  88.                             users.Id = (int)reader[0];  
  89.                             users.Username = (string)reader[1];  
  90.                             users.Sex = (int)reader[2];  
  91.                             users.Age = (int)reader[3];  
  92.                             users.Address = (string)reader[4];  
  93.   
  94.                             UserList.Add(users);  
  95.                         }  
  96.                     }  
  97.   
  98.                 }  
  99.   
  100.             }  
  101.   
  102.             return UserList;  
  103.         }  
  104.     }  
  105. }  

4. Users.cs, 用於存儲數據
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DataGridViewTest2  
  7. {  
  8.     class User  
  9.     {  
  10.         private int id, sex, age;  
  11.         private string username, address;  
  12.   
  13.         public int Id  
  14.         {  
  15.             get { return id; }  
  16.             set { id = value; }  
  17.         }  
  18.   
  19.         public int Sex  
  20.         {  
  21.             get { return sex; }  
  22.             set { sex = value; }  
  23.         }  
  24.   
  25.         public int Age  
  26.         {  
  27.             get { return age; }  
  28.             set { age = value; }  
  29.         }  
  30.   
  31.         public string Username  
  32.         {  
  33.             get { return username; }  
  34.             set { username = value; }  
  35.         }  
  36.   
  37.         public string Address  
  38.         {  
  39.             get { return address; }  
  40.             set { address = value; }  
  41.         }  
  42.     }  
  43. }  

5. 數據庫 的兩個表

UserLogin



UserInfo



6. 登錄成功後顯示的結果如下:



update:

從數據庫裏提取信息的代碼有所更新, 使用 SqlDataAdapter來查詢數據,並將數據填充到DataTable裏反回給DataGridView裏顯示.

  1. public DataTable ExportData()    
  2.         {    
  3.             DataTable UserTable = new DataTable();   
  4.             using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserInfo.mdf;Integrated Security=True;User Instance=True"))    
  5.             {    
  6.                 using (SqlCommand cmd = conn.CreateCommand())    
  7.                 {    
  8.                     cmd.CommandText = "SELECT * FROM UserInfo";    
  9.                     using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))    
  10.                     {    
  11.                           SqlDataAdapter.Fill(UserTable)  
  12.                      }    
  13.                 }    
  14.             }    
  15.             return UserTable;    
  16.         }    
  17.     }    


發佈了47 篇原創文章 · 獲贊 124 · 訪問量 44萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章