首页 > 其他分享 >net framwork winform

net framwork winform

时间:2023-03-31 15:58:53浏览次数:37  
标签:mySqlCommand studentId framwork dataGridView1 student new mySqlConnection net wi

winform是窗体项目

该winform 项目使用的数据库是mysql,vs版本是2022

创建一个net framework项目

 

随便选一个框架版本

 

 

将form窗体中加入  一个DataGridView、四个button(新增、更新、删除、查询)按钮、一个textbox。这些控件在  视图---工具栏  

 

 

 

把窗体中的button按钮名字都改掉,方便查看

 

 

在项目中创建一个student实体类


/// <summary>
/// 学号
/// </summary>
public int studentId { get; set; }


/// <summary>
/// 姓名
/// </summary>
public string sName { get; set; }


/// <summary>
/// 年龄
/// </summary>
public int sAge { get; set; }


/// <summary>
/// 爱好
/// </summary>
public string sLike { get; set; }


public Student(int studentId, string sName, int sAge, string sLike)
{
this.studentId = studentId;
this.sName = sName;
this.sAge = sAge;
this.sLike = sLike;
}

 

相对应mysql库中也要建一个student表,现在我们先手动创建数据库表,后期再给大家说EF映射

写语句创建库

CREATE DATABASE StudentCase

 

 

 创建学生表student

CREATE TABLE  student(

studentid INT PRIMARY KEY,
sname VARCHAR(100) ,
sage INT ,
slike VARCHAR(200)


)

 

 

因为用的是mysql数据库,所以要引用mysql在net framwork 中对应的dll文件:MySql.Data

 

 

新增按钮是跳转到另外的表单上面,所以我们再创建一个  StudentManager 窗体  (表单创建4个label、4个textbox、1个button)

 

删改查代码

 public partial class StudentManager : Form
    {
        public List<Student> students = null;
        public StudentManager()
        {
            InitializeComponent();
        }
        private string connectionStr = "server=127.0.0.1;port=3306;user=root;password=root;database=studentcase";//连接数据库
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void edit_Click(object sender, EventArgs e)
        {
            
            //dataGridView1.DataSource.students
            Student student= students[dataGridView1.CurrentRow.Index];
            string editStr = $"update student set studentId={dataGridView1["studentId", dataGridView1.CurrentRow.Index].Value},sName='{dataGridView1["sName", dataGridView1.CurrentRow.Index].Value}',sAge={dataGridView1["sAge", dataGridView1.CurrentRow.Index].Value},sLike='{dataGridView1["sLike",dataGridView1.CurrentRow.Index].Value}' where studentId='{student.studentId}'";
            MySqlConnection mySqlConnection = new MySqlConnection(connectionStr);
            mySqlConnection.Open();
            MySqlCommand mySqlCommand = new MySqlCommand(editStr, mySqlConnection);
            
            mySqlCommand.ExecuteNonQuery();//对连接执行SQL语句并返回行数
            mySqlCommand.Dispose();//释放MySqlCommand资源
            mySqlConnection.Close();

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void delete_Click(object sender, EventArgs e)
        {
            Student student = students[dataGridView1.CurrentRow.Index];
            string delStr = $"delete from student where studentId={dataGridView1["studentId", dataGridView1.CurrentRow.Index].Value}";
            MySqlConnection mySqlConnection = new MySqlConnection(connectionStr);
            mySqlConnection.Open();
            MySqlCommand mySqlCommand = new MySqlCommand(delStr,mySqlConnection);
           
            mySqlCommand.ExecuteNonQuery();
            mySqlCommand.Dispose();
            mySqlConnection.Close();

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            new AddOrEdit().Show();//指定展示控件

        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void query_Click(object sender, EventArgs e)
        {
            string QueryStr = "select * from student";
            MySqlConnection mySqlConnection = new MySqlConnection(connectionStr);
            MySqlCommand mySqlCommand = new MySqlCommand(QueryStr, mySqlConnection);
            mySqlConnection.Open();
            MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
            
            students = new List<Student>();
            while (mySqlDataReader.Read())
            {
                students.Add(new Student(int.Parse(mySqlDataReader[0].ToString()), mySqlDataReader[1].ToString(), int.Parse(mySqlDataReader[2].ToString()), mySqlDataReader[3].ToString()));

            }
            mySqlCommand.Dispose();
            mySqlConnection.Close();
            dataGridView1.DataSource = students;
        }
    }

添加代码

public partial class AddOrEdit : Form
    {
        
        public AddOrEdit()
        {
            InitializeComponent();
        }
        private string connectionStr = "server=127.0.0.1;port=3306;user=root;password=root;database=studentcase";//连接数据库

        private void textstudentId_TextChanged(object sender, EventArgs e)
        {
            
            //var reader = mySqlCommand.ExecuteReader();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string addStr = $"insert into student(studentId,sName,sAge,sLike) values({int.Parse(textstudentId.Text)},'{textname.Text}',{int.Parse(textage.Text)},'{textlike.Text}');";
            MySqlConnection mySqlConnection = new MySqlConnection(connectionStr);


            MySqlCommand mySqlCommand = new MySqlCommand(addStr, mySqlConnection);
            mySqlConnection.Open();
            mySqlCommand.ExecuteNonQuery();
            mySqlCommand.Dispose();
            mySqlConnection.Close();

            textstudentId.Text = "";//清空text内容
            textname.Text = "";
            textage.Text = "";
            textlike.Text = "";
        }
    }

 

 

本节遇到的问题

 

“张三“  是四个字节一个单位进行编码的,而我们通常使用的utf-8编码在mysql数据库中默认是按照3个字节一个单位进行编码的,正是这个原因导致将数据存入mysql数据库的时候出现错误

 解决方法:输入失sql语句alter table `student` convert to character set utf8mb4

 

 

内容下载地址:

链接:https://pan.baidu.com/s/1b2BMgAqBbKgjA2w--e_Beg
提取码:cudg

标签:mySqlCommand,studentId,framwork,dataGridView1,student,new,mySqlConnection,net,wi
From: https://www.cnblogs.com/ererjie520/p/17266383.html

相关文章

  • 解决videojs 在Chrome浏览器下报:A network error caused the media download to fail
    记录一下videoJS在Chrome浏览器下有时候出现播放一半或者回退的一个恶心bug,错误提示如下:Anetworkerrorcausedthemediadownloadtofailpart-way.经过一下午的折腾查找,终于在GitHub上看到他们官方的一个解决方案,这个方案目前没有更新在官方文档最新版本中,只是随便提了一下......
  • CSharp: Tesseract OCR V5.0 in donet core 3.1
    Referenceresourceshttps://github.com/alex-doe/open-ocr-dotnethttps://github.com/tleyden/open-ocr/gohttps://github.com/DayBreak-u/chineseocr_litehttps://github.com/pjreddie/darknethttps://sourceforge.net/projects/vietocr/https://github.com/PaddlePaddle/......
  • 基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试 - 3/3
    基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-1/3基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-2/3基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-3/3项目地址:https://github.com/janrs-io/Jgrpc转载请注明来源:https://janrs.com/6rdh......
  • 基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试 - 2/3
    基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-1/3基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-2/3基于Go/Grpc/kubernetes/Istio开发微服务的最佳实践尝试-3/3项目地址:https://github.com/janrs-io/Jgrpc转载请注明来源:https://janrs.com/ugj7......
  • base64 之坑----Base64后出现换行符 org.apache.commons.net.util.Base64()
    坑记录问题:base64后的结果会出现\r\n换行符, 复现:publicstaticvoidmain(String[]args){Stringkey="ssjsi21djsiej284858jweejrh34981dwde32323243232423423412121";Strings=java.util.Base64.getEncoder().encodeToString(key.getBytes(St......
  • Asp.Net Framework项目优化前端文件引用
    背景:公司一个老项目,前端引用都是直接引用文件,这样导致每次发布都存在用户浏览器缓存问题,常见做法是找到每个引用前端文件处加?version,但是太麻烦了解决思路:1.找到所有前端文件2.jenkins每次编译更新版本号,把新的版本号拼接到前端文件引用处实现:1.首先在扩展类里面扩展以......
  • Asp.Net Core 动态生成WebApi
    在WebApi架构体系中,一般需要先编写应用服务实现,再通过编写Controller来实现应用服务的Web接口。Controller中的代码作用仅仅是调用Service的方法,将Service提升为Web接口,其实完全可以通过动态生成WebApi来减少编码工作。在.Net示例项目ABP中已经实现了动态生成WebApi的功能,Panda.Dy......
  • Elasticsearch.Net+Nest基本用法
    基本用法安装Nest(安装后默认会装上Elasticsearch.Net),注意:版本尽量选择跟ES版本一致的批量初始化数据到ESusingNest;try{//测试环境配置SSL证书需要的设置ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12|SecurityProtocolType.Tls11......
  • 新Apple TV支持Netflix直接注册、iTunes账户付费
    今天凌晨,苹果发布了新款机顶盒AppleTV。升级后的AppleTV允许用户直接从该设备上注册Netflix,使用iTunes账户支付该流服务订阅费用。据路透社昨天报道,Hastings最近在寻求与付费电视运营商合作,希望能捆绑其服务,通过有线电视运营商的机顶盒提供电影电视节目。据知情人士透露,对Netfl......
  • 界面组件Telerik ASP.NET MVC使用指南 - 如何自定义网格过滤(一)
    TelerikUIforASP.NETMVC拥有使用JavaScript和HTML5构建网站和移动应用所需的70+UI部件,来满足开发者的各种需求,提供无语伦比的开发性能和用户体验。它主要是针对专业级的ASP.NET开发,通过该产品的强大功能,开发者可以开发出功能丰富、适应标准广泛的响应式应用程序。对于任何......