c winform mysql类_winform+mysql入门学习

毕业后,第一份工作是写单片机(C)代码的,这一份也是写C代码的(emmmmmmmmm,虽然入职两个多月了一行代码也没写过啊哈哈哈哈),所以好久没写过博客了,周末没事干(没有女朋友,单身23年呜呜呜呜呜)就学了下C#和MySQL,然后就写了个最简单的类似于“hello world”的小项目-图书管理系统,这部分博文只有用户管理部分的,这篇文章主要是记录自己的学习过程。

c winform mysql类_winform+mysql入门学习_第1张图片

C#没什么好说的,只不过是一种面向对象编程语言,大学期间上课不认真听讲学习的,啊哈哈哈哈,MySQL是在bilibili看某智博客的视频学的,都算是半路出家的,所以博文水平低,质量差大佬勿喷,当然这也是我自己学习的记录,大佬可以细心指教,千恩万谢。

首先说下C#的三层架构,如下这段话是从大佬的博客里面学习的:

三层架构分为:表现层(UI(User Interface))、业务逻辑层(BLL(Business Logic Layer))、数据访问层(DAL(Data Access Layer))再加上实体类库(Model)。实体类库(Model),主要存放数据库中的表字段;数据访问层(DAL),主要是存放对数据类的访问,即对数据库的添加、删除、修改、更新等基本操作;业务逻辑层(BLL)对传送数据进行逻辑判断分折,并进行传送正确的值;表现层(UI)即用户界面层。

如下图所示,是我的工程文件结构,2_StudentForm是表示(UI)层,添加的是Windows窗体应用程序;StudentBLL是业务逻辑层,StudentDAL是数据访问层,StudentModel是实体类库,这三个均是类库。

c winform mysql类_winform+mysql入门学习_第2张图片

UI层没什么好说的,我这种菜鸟还是喜欢拖控件,然后写需要的事件就行。

Model层与数据库字段表相对应,如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace StudentModel

{

public class UserInfoModel

{

///

/// 用户ID

///

public int Id { get; set; }

///

/// 用户名

///

public string Name { get; set; }

///

/// 密码

///

public string Pwd { get; set; }

}

}

DAL是数据库操作的相关代码,在DAL类库中引用Model类库,我就写了最基础的sql操作代码,如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using StudentModel;

using MySql.Data.MySqlClient;

using System.Data;

namespace StudentDAL

{

public class UserInfoDAL

{

const string str = "server = localhost;user = root;password=1234;database = student";

MySqlConnection conn = new MySqlConnection(str);

///

/// 从数据库查找用户

///

///

///

public UserInfoModel MySqlSelectUser(UserInfoModel _userInfoModel)

{

conn.Open();

string sqlstr = String.Format("select * from userinfo where name = '{0}' and pwd = '{1}'", _userInfoModel.Name, _userInfoModel.Pwd);

MySqlCommand cmd = new MySqlCommand(sqlstr, conn);

MySqlDataReader reader = cmd.ExecuteReader();

UserInfoModel user = new UserInfoModel();

if(reader.Read())

{

user.Id = reader.GetInt32(0);

user.Name = reader.GetString(1);

user.Pwd = reader.GetString(2);

}

conn.Close();

return user;

}

///

/// 从数据库查找用户名

///

///

///

public UserInfoModel MySqlSelectName(UserInfoModel _userInfoModel)

{

conn.Open();

string sqlstr = String.Format("select * from userinfo where name = '{0}'", _userInfoModel.Name);

MySqlCommand cmd = new MySqlCommand(sqlstr, conn);

MySqlDataReader reader = cmd.ExecuteReader();

UserInfoModel user = new UserInfoModel();

if (reader.Read())

{

user.Id = reader.GetInt32(0);

user.Name = reader.GetString(1);

user.Pwd = reader.GetString(2);

}

conn.Close();

return user;

}

///

/// 查询所有数据

///

///

public List MySqlSelectUserAll()

{

List list = new List();

conn.Open();

string sqlstr = "select * from userinfo";

DataTable dt = new DataTable();

MySqlDataAdapter rd = new MySqlDataAdapter(sqlstr, conn);

rd.Fill(dt);

foreach (DataRow dr in dt.Rows)

{

UserInfoModel ai = new UserInfoModel

{

Id = Convert.ToInt32(dr["Id"]),

Name = dr["Name"].ToString(),

Pwd = dr["Pwd"].ToString()

};

list.Add(ai);

}

conn.Close();

return list;

}

///

/// 删除用户

///

///

public int MySqlDelUser(UserInfoModel _userInfoModel)

{

conn.Open();

string sqlstr = String.Format("delete from userinfo where name = '{0}'", _userInfoModel.Name);

MySqlCommand cmd = new MySqlCommand(sqlstr, conn);

int iRet = cmd.ExecuteNonQuery();

conn.Close();

return iRet;

}

///

/// 插入数据

///

///

///

public int MySqlInsertUser(UserInfoModel _userInfoModel)

{

conn.Open();

string sqlstr = String.Format("insert into userinfo (name,pwd) values('{0}','{1}')",_userInfoModel.Name,_userInfoModel.Pwd);

MySqlCommand cmd = new MySqlCommand(sqlstr, conn);

int iRet = cmd.ExecuteNonQuery();

conn.Close();

return iRet;

}

///

/// 更新数据

///

///

///

public int MySqlUpdateUserPwd(UserInfoModel _userInfoModel)

{

conn.Open();

string sqlstr = String.Format("update userinfo set Pwd = '{0}' where name = '{1}'",_userInfoModel.Pwd,_userInfoModel.Name);

MySqlCommand cmd = new MySqlCommand(sqlstr, conn);

int iRet = cmd.ExecuteNonQuery();

conn.Close();

return iRet;

}

}

}

BLL是界面操作所做的相关处理,在BLL类库中引用DAL和Model类库,如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using StudentDAL;

using StudentModel;

namespace StudentBLL

{

public class UserInfoBLL

{

///

/// 判断用户是否存在

///

///

///

///

public bool CheckUserExist(string name,string pwd)

{

UserInfoModel user = new UserInfoModel();

user.Name = name;

user.Pwd = pwd;

UserInfoDAL userInfoDAL = new UserInfoDAL();

UserInfoModel userInfoModel = userInfoDAL.MySqlSelectUser(user);

if (userInfoModel.Name == name && userInfoModel.Pwd == pwd)

{

return true;

}

else

{

return false;

}

}

///

/// 判断用户名是否存在

///

///

///

public bool CheckUserName(string name)

{

UserInfoModel user = new UserInfoModel();

user.Name = name;

UserInfoDAL userInfoDAL = new UserInfoDAL();

UserInfoModel userInfoModel = userInfoDAL.MySqlSelectName(user);

if (userInfoModel.Name == name)

{

return true;

}

else

{

return false;

}

}

///

/// 查看所有的用户

///

public List CheckAllUser()

{

List userInfoModel = new List();

List list = new List();

UserInfoDAL userInfoDAL = new UserInfoDAL();

userInfoModel = userInfoDAL.MySqlSelectUserAll();

for (int i = 0; i < userInfoModel.Count; i++)

{

list.Add(userInfoModel[i].Name);

}

return list;

}

///

/// 删除所选中的用户

///

public bool DelSelUser(string selname)

{

UserInfoModel userinfomodel = new UserInfoModel();

UserInfoDAL userinfodal = new UserInfoDAL();

userinfomodel.Name = selname;

if (userinfodal.MySqlDelUser(userinfomodel) > 0)

{

return true;

}

else

{

return false;

}

}

///

/// 添加用户

///

///

///

public int InsertUser(string name, string pwd)

{

UserInfoModel userinfomodel = new UserInfoModel();

UserInfoDAL userinfodal = new UserInfoDAL();

userinfomodel.Name = name;

userinfomodel.Pwd = pwd;

if (CheckUserName(name) == true)

{

return -1;

}

else

{

return userinfodal.MySqlInsertUser(userinfomodel);

}

}

///

/// 更新用户密码

///

///

///

public bool UpdateUserPwd(string name,string pwd)

{

UserInfoModel userinfomodel = new UserInfoModel();

UserInfoDAL userinfodal = new UserInfoDAL();

userinfomodel.Name = name;

userinfomodel.Pwd = pwd;

if (userinfodal.MySqlUpdateUserPwd(userinfomodel) > 0)

{

return true;

}

else

{

return false;

}

}

}

}

代码已上传至CSDN,链接如下:

https://download.csdn.net/download/qq_28091109/11292751

我是半路出家的外行,这也只是我个人学习记录,大佬勿喷

你可能感兴趣的:(c,winform,mysql类)