书犹药也,善读可以医愚

    

C#三层架构实现WinForm连接Mysql显示数据库数据实例

首先创建数据库丨 学号 姓名 班级 QQ 微信 丨5列

三层架构,即包含 BLL,DAL,Model,DBHelper,Winform

必须 Mysql 包
using MySql.Data.MySqlClient;
请依照下列文件规范命名


  1. 首先配置WinForm下 App.config 文件(Mysql数据库连接为例)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name ="NiceHanMySql" connectionString="server=数据库地址;port=端口号;user=数据库用户名;password=密码; database=数据库名;"/>
  </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>
  1. BLL 下 NiceBLL.cs文件代码

using DAL;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
   public class NiceBLL
    {
        NiceDAL tableDal = new NiceDAL();
        public List<NiceModel> GetTableList()
        {
            return tableDal.GetTablelist();
        }
        public String NiceFormText()
        {
            return "NiceHan Mysql TextDate";
        }
        public String SystemonLock()
        {
            return "NiceHanWinForm";
        }
    }
}
  1. DAL 下 NiceDAL.cs文件代码

using Models;
using SqlDbHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class NiceDAL
    {
        public List<NiceModel> GetTablelist()
        {
            string sqlStr = "select * from PZ ";
            DataTable sTable = NiceDB.GetDataTable(sqlStr, null);
            List<NiceModel> tableList = new List<NiceModel>();
            foreach (DataRow dr in sTable.Rows)
            {
                NiceModel tb = new NiceModel();
                tb.学号 = (dr["Num"].ToString());
                tb.姓名 = (dr["Name"].ToString());
                tb.班级 = (dr["Class"].ToString());
                tb.QQ = (dr["QQ"].ToString());
                tb.微信 = (dr["Wx"].ToString());
                tableList.Add(tb);
            }
            return tableList;
        }
    }
}
  1. Model 下 NiceModel.cs文件代码

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

namespace Models
{
    public class NiceModel
    {
        public string 姓名 { set; get; }
        public string 学号 { set; get; }
        public string 班级 { set; get; }
        public string QQ { set; get; }
        public string 微信 { set; get; }
    }
}
  1. DBhelper 下 NiceDB.cs文件代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using MySql.Data.MySqlClient;

namespace SqlDbHelper
{
    public static class NiceDB
    {
        private static readonly string connStr = ConfigurationManager.ConnectionStrings["NiceHanMySql"].ConnectionString;

        public static int ExecuteNoneQuery(string sqlStr, params System.Data.SqlClient.SqlParameter[] pms)
        {
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                using (MySqlCommand cmd = new MySqlCommand(sqlStr, conn))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        public static object ExecuteScalar(string sqlStr, params MySqlParameter[] pms)
        {
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                using (MySqlCommand cmd = new MySqlCommand(sqlStr, conn))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    conn.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }
        public static MySqlDataReader ExecuteReader(string sqlStr, params MySqlParameter[] pms)
        {
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                using (MySqlCommand cmd = new MySqlCommand(sqlStr, conn))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    try
                    {
                        conn.Open();
                        return cmd.ExecuteReader();
                    }
                    catch
                    {
                        conn.Close();
                        conn.Dispose();
                        throw;
                    }
                }
            }
        }
        public static DataTable GetDataTable(string sqlStr, params MySqlParameter[] pms)
        {
            using (MySqlDataAdapter adapter = new MySqlDataAdapter(sqlStr, connStr))
            {
                DataTable dt = new DataTable();
                if (pms != null)
                {
                    adapter.SelectCommand.Parameters.AddRange(pms);
                }
                adapter.Fill(dt);
                return dt;
            }
        }
    }
}
  1. Winform 下 NiceHanForm.cs文件代码

using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void Form2_Load(object sender, EventArgs e)
        {
            NiceBLL tb = new NiceBLL();
            this.Text = tb.SystemonLock();
            this.WinForm.Text = tb.NiceFormText();
            this.dataGridView1.DataSource = tb.GetTableList();
        }
    }
}
  1. 恭喜你完成 运行调试即可

添加新评论

  时光Time

岂能尽随人愿,但求无愧我心。
--- updated on 2019-11-23 12:52

  关于博主

98年,处女座,有一点点点点强迫症,性格诡异,情绪复杂多变。爱好码代码。也喜欢听歌与阅读,喜欢接触新鲜事物。座右铭:活到老,学到老,生命在于学习!

  近期评论

生命要得到丰盛的收获,必需阳光;同时,也需要“雨”的浇灌。

人生应该树立目标,否则你的精力会白白浪费。

山涧的泉水经过一路曲折,才唱出一支美妙的歌。

如若不是为了一个人,谁肯枯守一座城。城市和爱情,总是有着这样那样的关系。我们会因为一个人,去到那座城,因为那是一座爱的城;我们也会因为一个人,离开一座城,那是一座绝望的伤城。

常求有利别人,不求有利自己。