C#中的MySQL

这篇博客演示了如何使用C#连接和操作MySQL数据库,包括插入数据、读取数据、更新数据、删除数据以及执行SQL聚合函数。示例代码详细展示了如何建立数据库连接、执行SQL命令和处理异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


namespace CSharpCallMySql
{
    class Program
    {
        static void Main(string[] args)
        {
            //Read();
            //Insert();
            //Update();
            //Delete();
            //ExcuteScala();
            Console.Write(VerifyUser("xiongpeng1", "xiongpeng"));
            Console.ReadKey();
        }

        public static void Insert()
        {
            string connCmd = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
            MySqlConnection conn = new MySqlConnection(connCmd);
            try
            {
                conn.Open();
                Console.WriteLine("连接成功");
                string cmdSql = "insert into users(userid, passwd) values('kangkang1', 'kangkang1');";
                Console.WriteLine(cmdSql);
                MySqlCommand cmd = new MySqlCommand(cmdSql, conn);
                int effectLines = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }

        public static void Read()
        {
            string str = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
            MySqlConnection con = new MySqlConnection(str);
            try
            {
                con.Open();
                Console.WriteLine("数据库连接成功!");
                string sql = "select * from users";
                MySqlCommand cmd = new MySqlCommand(sql, con);
                //cmd.ExecuteReader();
                //cmd.ExecuteScalar();
                //cmd.ExecuteNonQuery();
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    //Console.WriteLine(reader[0].ToString() + reader[1].ToString() + reader[2].ToString());
                    //Console.WriteLine(reader.GetInt32(0) + " " + reader.GetString(1) + " " + reader.GetString(2));
                    Console.WriteLine(reader.GetInt32("id") + " " + reader.GetString("userid") + " " + reader.GetString("passwd"));
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            finally
            {
                con.Close();
            }
            Console.ReadKey();
        }

        public static void Update()
        {
            string connCmd = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
            MySqlConnection conn = new MySqlConnection(connCmd);
            try
            {
                conn.Open();
                Console.WriteLine("连接成功");
                string cmdSql = "update users set userid = 'sunwukong', passwd = '123swk' where id = 1;";
                Console.WriteLine(cmdSql);
                MySqlCommand cmd = new MySqlCommand(cmdSql, conn);
                int effectLines = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }

        public static void Delete()
        {
            string connCmd = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
            MySqlConnection conn = new MySqlConnection(connCmd);
            try
            {
                conn.Open();
                Console.WriteLine("连接成功");
                string cmdSql = "delete from users where id = 5";
                Console.WriteLine(cmdSql);
                MySqlCommand cmd = new MySqlCommand(cmdSql, conn);
                int effectLines = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }

        public static void ExcuteScala()
        {
            string str = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
            MySqlConnection con = new MySqlConnection(str);
            try
            {
                con.Open();
                Console.WriteLine("数据库连接成功!");
                string sql = "select count(*) from users";
                MySqlCommand cmd = new MySqlCommand(sql, con);
                object o = cmd.ExecuteScalar();
                Console.WriteLine(Convert.ToInt32(o));
                Console.ReadKey();
                
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            finally
            {
                con.Close();
            }
            Console.ReadKey();
        }

        public static bool VerifyUser(string username, string password)
        {
            string str = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
            MySqlConnection con = new MySqlConnection(str);
            try
            {
                con.Open();
                Console.WriteLine("数据库连接成功!");
                string sql = "select * from users where userid = @para1 and passwd = @para2";//此处标记,下面通过cmd对应修改
                MySqlCommand cmd = new MySqlCommand(sql, con);
                cmd.Parameters.Add("para1", username);
                cmd.Parameters.Add("para2", password);

                MySqlDataReader reader = cmd.ExecuteReader();
                if(reader.Read())
                {
                    return true;
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            finally
            {
                con.Close();
            }
            Console.ReadKey();
            return false;
        }
    }
}
        
   



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值