CSharp直接连接MySQL

以下是测试源码:

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


namespace CSharp直接连接MySQL
{
    class Program
    {
        static void Main(string[] args)
        {
            //Insert();
            //Update();
            // Delete();
            //Read();


            //ReadUserCount();
            // ExcuteScalar();
            Console.WriteLine( VerifyUser("c", "d"));
            Console.ReadKey();
        }
        static bool  VerifyUser(string username,string password) //通过这个验证mysql中的数据库中是否存在账号密码
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库
            
            MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道  并没有跟数据库跟数据建立连接
            try
            {
                conn.Open();
                //string sql = "select *  from users where username='"+ username + "'and password='"+ password + "'"; //我们自己按照查询条件组拼mysql 很麻烦


                string sql = "select *  from users where username=@username and password=@password "; 


                MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令
                command.Parameters.AddWithValue("username", username);
                command.Parameters.AddWithValue("password", password);


                MySqlDataReader reader = command.ExecuteReader();
                if (reader.Read()) {
                    return true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                conn.Close();
            }
            return false;
        } //


        static void Read()
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库




            MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道  并没有跟数据库跟数据建立连接
            try
            {
                conn.Open();


                //string sql = "select id , password   from users";
                string sql= "select *  from users";
                MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令
                MySqlDataReader reader = command.ExecuteReader();
                //reader.Read();  //调用一次相当于翻一页书


                //Console.WriteLine(reader[0].ToString() + reader[1].ToString() + reader[2].ToString());


                while (reader.Read()) //如果有数据 返回True 没数据返回false
                {
                    //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("username")+ " "+reader.GetString("password")+""+reader[3].ToString());
                }


                //command.ExecuteReader(); //执行一些查询
                //command.ExecuteNonQuery(); //插入删除
                //command.ExecuteScalar();//执行一些查询,返回一个单个值


                Console.WriteLine("已经建立连接");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }


                    finally
                    {
                        conn.Close();
                    }


                    Console.ReadKey();
        }
        
        static void Insert()
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库




            MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道  并没有跟数据库跟数据建立连接
            try
            {
                conn.Open();


               // string sql = "insert into users(username,password) values('f','g')";
                string sql = "insert into users(username,password,time) values('f','g','"+DateTime.Now+"')";
                MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令
               // MySqlDataReader reader = command.ExecuteReader();
                int  result=    command.ExecuteNonQuery(); //返回的是数据库中受影响的数据的行数
                Console.WriteLine(result);
                //Console.WriteLine("已经建立连接");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


            finally
            {
                conn.Close();
            }


            Console.ReadKey();
        }
        
        static void Update()
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库




            MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道  并没有跟数据库跟数据建立连接
            try
            {
                conn.Open();


           
                string sql = "update  users set username='abc',password='222' where id='3'";
                MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令
                                                                    // MySqlDataReader reader = command.ExecuteReader();
                int result = command.ExecuteNonQuery(); //返回的是数据库中受影响的数据的行数
                Console.WriteLine(result);
                //Console.WriteLine("已经建立连接");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


            finally
            {
                conn.Close();
            }


            Console.ReadKey();
        }


        static void Delete()
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库




            MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道  并没有跟数据库跟数据建立连接
            try
            {
                conn.Open();




                string sql = "delete from users  where id='3'";
                MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令
                                                                    // MySqlDataReader reader = command.ExecuteReader();
                int result = command.ExecuteNonQuery(); //返回的是数据库中受影响的数据的行数
                Console.WriteLine(result);
                //Console.WriteLine("已经建立连接");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


            finally
            {
                conn.Close();
            }


            Console.ReadKey();
        }


        static void ReadUserCount()
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库




            MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道  并没有跟数据库跟数据建立连接
            try
            {
                conn.Open();


                //string sql = "select id , password   from users";
                string sql = "select count(*)  from users";
                MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令
                MySqlDataReader reader = command.ExecuteReader();
                reader.Read();
                int count = Convert.ToInt32(reader[0].ToString());


                Console.WriteLine(count);




                Console.WriteLine("已经建立连接");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


            finally
            {
                conn.Close();
            }


            Console.ReadKey();
        }


        static void ExcuteScalar()  //当返回值只有一个值的时候 使用ExcuteScalar 比较方便
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库




            MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道  并没有跟数据库跟数据建立连接
            try
            {
                conn.Open();


                //string sql = "select id , password   from users";
                string sql = "select count(*)  from users";
                MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令
                object reader = command.ExecuteScalar();
                
                int count = Convert.ToInt32(reader.ToString());


                Console.WriteLine(count);




                Console.WriteLine("已经建立连接");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


            finally
            {
                conn.Close();
            }


            Console.ReadKey();
        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诗远

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值