C#之MySql查詢

1.MySql查詢

(1)新建一個項目,windows窗體應用程序

(2)添加引用mysql.data.dll和命名空間

(3)在mysql數據庫創建表

這裏用到的是student表,主要字段有student_id,student_code,student_name,student_sex,student_age,description。

(4)創建一個類和數據庫中的表對應

類裏面寫屬性構造器,和數據庫中的表字段對應

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace demo
{
    public class Student
    {
        public int student_id { get; set; }
        public int student_code { get; set; }
        public string student_name { get; set; }
        public string student_sex { get; set; }
        public int student_age { get; set; }
        public string description { get; set; }
    }
}

(5)窗體設計

添加列,右邊的【數據】這裏綁定表中對應的字段,id 設爲不可見。

這裏寫圖片描述

(6)窗體加載事件

窗體加載後是顯示出結果

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string conStr = "Server=localhost;user=root;pwd=admin;database=test";
            //1.創建連接
            using (MySqlConnection myCon = new MySqlConnection(conStr)) 
            {

                 //2.打開
                 myCon.Open();
                string sql = "select * from student";
                //3.執行命令
                MySqlCommand cmd = new MySqlCommand(sql, myCon);
                //4.讀取結果
                MySqlDataReader read = cmd.ExecuteReader();
                //5.使用
                List<Student> stuList = new List<Student>();
                while (read.Read()) 
                {
                    //構造初始化器
                    stuList.Add(new Student()
                    {
                        student_id = Convert.ToInt32(read.GetValue(0).ToString()),
                        student_code = Convert.ToInt32(read.GetValue(1).ToString()),
                        student_name=read.GetValue(2).ToString(),
                        student_sex=read.GetValue(3).ToString(),
                        student_age=Convert.ToInt32(read.GetValue(4).ToString()),
                        description=read.GetValue(5).ToString()

                    });
                }
                dataGridView1.DataSource = stuList;

            }
        }
    }
}

C#是面向對象的思想,我們可以讀取到表裏面所有的字段值,如果把值一個一個傳給前臺肯定很麻煩,所以可以封裝一個對象,對象裏面傳好值,最後直接把對象傳給前臺就ok了φ(≧ω≦*)♪。stuList是一個對象集合,查詢到的每一條記錄都是一個對象,只需把對象添加到stuList集合裏,再把它和數據源進行綁定就成功了

(7)運行結果

這裏寫圖片描述

2.SqlHelper改寫

新建一個類庫

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace SqlHelper
{
    public static class sqlHelper
    {
        public static DataTable GetList(string sql) {

             string conStr = "Server=localhost;user=root;pwd=admin;database=test";
             using (MySqlConnection myCon = new MySqlConnection(conStr))
             {
                 MySqlDataAdapter adapter = new MySqlDataAdapter(sql, myCon);
                 DataTable table = new DataTable();
                 adapter.Fill(table);
                 return table;
             }
        }
    }
}

調用

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string sql = "select * from student where student_code";
            DataTable dt=SqlHelper.sqlHelper.GetList(sql);
            List<Student> stuList = new List<Student>();
            foreach (DataRow row in dt.Rows) {
                stuList.Add(new Student()
                {

                    student_id = Convert.ToInt32(row["student_id"]),
                    student_code = Convert.ToInt32(row["student_code"]),
                    student_name = row["student_name"].ToString(),
                    student_sex = row["student_sex"].ToString(),
                    student_age = Convert.ToInt32(row["student_age"]),
                    description = row["description"].ToString()
                });

            }
            dataGridView1.DataSource = stuList;


        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章