CSharp直接连接MySQL

来源:互联网 发布:js算法题 编辑:程序博客网 时间:2024/06/06 19:04

以下是测试源码:

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();        }    }}

原创粉丝点击