Windows程序设计——基于c#使用VS实现Mysql数据库操作

本篇的内容主要是,如何使用Vs设计窗体应用,同时连接mysql数据库,进行基础的增删改查操作。本人Vs版本为2019版。

一、程序窗体界面展示

        如下图所示,其中使用了textBox、label、comboBox和dataGridView几种控件。其dataGridView主要是用于展示我们的数据信息的控件。

        同时要说明,此案例并不全面,文本框控制只是在某种限定下可以操作,但是如果使用sql语句操作,则没有任何问题。

二、Vs使用mysql数据的相关说明

1.创建数据连接

Step.1

 Step.2

 Step.3

 Step.4

 Step.5

 Step.6

 Step.7

如果没有mysql选项

下载驱动:(这里下载第二个就可以了)

1.mysql-connector-odbc-8.0.20-winx64.msi   mysql odbc驱动

2.mysql-for-visualstudio-1.2.9.msi Visual Studio连接MySQL工具

3.mysql-connector-net-8.0.20.msi  mysql数据库.net开发驱动

Step.8

 Step.9

 Step.10

Step.11

 Step.12

         安装完成后,不用设置任何东西,但是保证你用过mysql,并且建立过数据库连接。然后直接打开VS。

 Step.13

 

2、添加mysql的依赖包

 Step.14

Step.15

三、功能说明

1、连接数据库

这里在连接的时候,个人娱乐,会默认查出welcome表。

 【代码段】

        private void button1_Click(object sender, EventArgs e)
        {
            
            string connstr = @"database = school; Password = 20001017; User ID = root; server = localhost; pooling = false; charset = utf8";
            //在外层定义了
            conn = new MySqlConnection(connstr);
            conn.Open();
            MessageBox.Show("连接成功");

            //查询welcom表
            string sql = "select* from welcome";
            adapter = new MySqlDataAdapter(sql, conn);
            //数据集、本地微型数据库可以存储多张表。
            set = new DataSet();
            //从数据库的stuinfo表中取出数据
            adapter.Fill(set, "welcome");
            //将取出的数据做为dataGridView1的数据源
            dataGridView1.DataSource = set;
            dataGridView1.DataMember = "welcome";
        }

2、查询

(1)查询所有内容

        未选择表时,无法查询。

        选择员工表,点击查询

(2)按照工号查询

        输入工号,然后查询

(3)SQL语句查询

 【工号和sql都有语句时】

 【代码】

        private void button2_Click(object sender, EventArgs e)
        {
            if(conn==null)
            {
                MessageBox.Show("请建立连接!");
                return;
            }

            if (comboBox1.Text == "")
            {
                MessageBox.Show("请选择一张表");
                return;
            }

            if(textBox1.Text == "" && textBox7.Text == "")
            {
                string sql = "select* from "+comboBox1.Text;
                adapter = new MySqlDataAdapter(sql, conn);
                //数据集、本地微型数据库可以存储多张表。
                set = new DataSet();
                //从数据库的stuinfo表中取出数据
                adapter.Fill(set, "welcome");
                //将取出的数据做为dataGridView1的数据源
                dataGridView1.DataSource = set;
                dataGridView1.DataMember = "welcome";
            }else if(textBox1.Text != "" && textBox7.Text != "")
            {
                MessageBox.Show("请选择按照主键值查询或者sql命令查询");
                return;
            }else if(textBox1.Text != "" && textBox7.Text == "")
            {
                string sql = "select * from " + comboBox1.Text + " where "+label1.Text+" = "+'"'+textBox1.Text+'"';
                Console.WriteLine(sql);
                adapter = new MySqlDataAdapter(sql, conn);
                //数据集、本地微型数据库可以存储多张表。
                set = new DataSet();
                //从数据库的stuinfo表中取出数据
                adapter.Fill(set, "welcome");
                //将取出的数据做为dataGridView1的数据源
                dataGridView1.DataSource = set;
                dataGridView1.DataMember = "welcome";

            }else if(textBox1.Text == "" && textBox7.Text != "")
            {
                string sql = textBox7.Text;
                Console.WriteLine(sql);
                adapter = new MySqlDataAdapter(sql, conn);
                //数据集、本地微型数据库可以存储多张表。
                set = new DataSet();
                //从数据库的stuinfo表中取出数据
                adapter.Fill(set, "welcome");
                //将取出的数据做为dataGridView1的数据源
                dataGridView1.DataSource = set;
                dataGridView1.DataMember = "welcome";
            }


        }

3、增加

(1) 未选择增加方式时

(2)文本框增加

         其中,重置可以清除文本框,点击查询即可查询所有。

(3)sql增加

 “1208“是中途增加了一条,故多了一条。

 【代码】

       private void button3_Click(object sender, EventArgs e)
        {
            int result = 0;

            if (conn == null)
            {
                MessageBox.Show("请建立连接!");
                return;
            }

            try
            {
                if (textBox1.Text != "" && textBox7.Text == "")
                {
                    string sql = "insert into " + comboBox1.Text + "(" + label1.Text + "," + label2.Text + "," + label3.Text + "," + label4.Text + "," + label5.Text + "," + label6.Text + ")" + " values(" + '"' + textBox1.Text + '"' + "," + '"' + textBox2.Text + '"' + "," + '"' + textBox3.Text + '"' + "," + '"' + textBox4.Text + '"' + "," + '"' + textBox5.Text + '"' + "," + '"' + textBox6.Text + '"' + ")";
                    //数据集、本地微型数据库可以存储多张表。
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    result = cmd.ExecuteNonQuery();
                }
                else if (textBox1.Text == "" && textBox7.Text != "")
                {
                    string sql = textBox7.Text;
                    //数据集、本地微型数据库可以存储多张表。
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    result = cmd.ExecuteNonQuery();
                }
                else if (textBox1.Text == "" && textBox7.Text == "")
                {
                    MessageBox.Show("请选择按照主键值删除或者sql命令增加!");
                    return;
                }

            }
            catch(Exception)
            {
                MessageBox.Show("操作失败,请检查操作或数据表");
            }

            if (result == 0)
            {
                MessageBox.Show("操作失败");
            }
            else
            {
                MessageBox.Show("增加成功!");
            }
        }

4、删除

(1)文本框以主键值删除

(2)sql删除

 【代码】

  private void button4_Click(object sender, EventArgs e)
        {
            int result=0;

            if (conn == null)
            {
                MessageBox.Show("请建立连接!");
                return;
            }


            if (textBox1.Text != "" && textBox7.Text == "")
            {
                string sql = "delete from " + comboBox1.Text + " where " + label1.Text + "=" + '"' + textBox1.Text + '"';
                //数据集、本地微型数据库可以存储多张表。
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                result = cmd.ExecuteNonQuery();
            }else if(textBox1.Text == "" && textBox7.Text != "")
            {
                string sql = textBox7.Text;
                //数据集、本地微型数据库可以存储多张表。
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                result = cmd.ExecuteNonQuery();
            }else if(textBox1.Text == "" && textBox7.Text == "")
            {
                MessageBox.Show("请选择按照主键值删除或者sql命令删除!");
                return;
            }
            

            if (result == 0)
            {
                MessageBox.Show("操作失败");
            }
            else
            {
                MessageBox.Show("删除成功!");
            }


        }

5、修改

(1)文本框修改

(2)Sql修改

这里失误忘记设置条件了!

【代码】

 private void button5_Click(object sender, EventArgs e)
        {
            if (conn == null)
            {
                MessageBox.Show("请建立连接!");
                return;
            }

            int result = 0;

            if (conn == null)
            {
                MessageBox.Show("请建立连接!");
                return;
            }

            try
            {
                if (textBox1.Text != "" && textBox7.Text == "")
                {
                    string sql = "update " + comboBox1.Text + " set " +
                        label2.Text + " = " + '"' + textBox2.Text + '"' + "," +
                        label3.Text + " = " + '"' + textBox3.Text + '"' + "," +
                        label4.Text + " = " + '"' + textBox4.Text + '"' + "," +
                        label5.Text + " = " + '"' + textBox5.Text + '"' + "," +
                        label6.Text + " = " + '"' + textBox6.Text + '"' + " " +
                        " where " + label1.Text + " = " + '"' + textBox1.Text + '"';
                    //数据集、本地微型数据库可以存储多张表。
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    result = cmd.ExecuteNonQuery();
                }
                else if (textBox1.Text == "" && textBox7.Text != "")
                {
                    string sql = textBox7.Text;
                    //数据集、本地微型数据库可以存储多张表。
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    result = cmd.ExecuteNonQuery();
                }
                else if (textBox1.Text == "" && textBox7.Text == "")
                {
                    MessageBox.Show("请选择按照文本框删除或者sql命令修改!");
                    return;
                }
            }
            catch(Exception)
            {
                MessageBox.Show("操作失败,请检查操作或数据表");
            }

           

            if (result == 0)
            {
                MessageBox.Show("操作失败");
            }
            else
            {
                MessageBox.Show("更新成功!");
            }


        }

6、切换其他表

【图书】

 【销售表】


 

总结

本篇主要是关于使用c#,通过窗体来控制使用mysql实现增删改查,此案例并不完善,但可提供思路。

本篇gitee:https://gitee.com/with-zero/windows--well-c--sql--vs.git

With_Zero 2022.05.27

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要安装 MySQL Connector/NET,这是一个官方提供的 MySQL 数据库连接器,可以让你使用 C#MySQL 进行交互。 在 Visual Studio 中,你需要引用以下命名空间: ```csharp using MySql.Data.MySqlClient; ``` 接下来,我们可以创建一个用于连接到数据库MySqlConnection 对象。在构造函数中,我们需要传入 MySQL 数据库的连接字符串。 ```csharp MySqlConnection connection = new MySqlConnection("server=localhost;database=mydatabase;uid=myusername;password=mypassword;"); ``` 其中,server 表示 MySQL 服务器的地址,database 表示要连接的数据库名称,uid 和 password 则是登录 MySQL 服务器的用户名和密码。 接下来,我们可以使用 MySqlCommand 对象来执行 SQL 命令。例如,以下代码会创建一个名为 `users` 的表。 ```csharp MySqlCommand command = new MySqlCommand("CREATE TABLE users (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(50), email VARCHAR(50), PRIMARY KEY(id))", connection); connection.Open(); command.ExecuteNonQuery(); connection.Close(); ``` 下面是一个实现增删改查的示例: ```csharp using System; using MySql.Data.MySqlClient; namespace MySqlTest { class Program { static void Main(string[] args) { string connectionString = "server=localhost;database=mydatabase;uid=myusername;password=mypassword;"; MySqlConnection connection = new MySqlConnection(connectionString); try { connection.Open(); // 添加一条记录 MySqlCommand insertCommand = new MySqlCommand("INSERT INTO users (name, email) VALUES ('张三', '[email protected]')", connection); insertCommand.ExecuteNonQuery(); // 查询记录 MySqlCommand selectCommand = new MySqlCommand("SELECT * FROM users", connection); MySqlDataReader reader = selectCommand.ExecuteReader(); while (reader.Read()) { Console.WriteLine("id: {0}, name: {1}, email: {2}", reader["id"], reader["name"], reader["email"]); } reader.Close(); // 修改记录 MySqlCommand updateCommand = new MySqlCommand("UPDATE users SET name='李四' WHERE email='[email protected]'", connection); updateCommand.ExecuteNonQuery(); // 删除记录 MySqlCommand deleteCommand = new MySqlCommand("DELETE FROM users WHERE email='[email protected]'", connection); deleteCommand.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { connection.Close(); } Console.ReadLine(); } } } ``` 在上面的示例中,我们先连接到 MySQL 数据库,然后执行了增、删、改、查的操作,并输出相应的结果。 注意,这只是一个简单的示例,实际情况下你需要根据自己的需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值