Unity网络编程二 : 服务器端连接数据库(C#连接MySQL)

服务端需要连接数据库才能与客户端进行数据的交互,本案例使用Mysql作为连接数据库

开发环境:

  • VS 2019
  • MySQL
  • .Net
    一、连接MySQL环境配置

为了连接MySQL,需要配置文件MySql.Data.dll,如果使用的是Vs2019,可以直接下载使用,不需要进行自己下载配置, 具体配置方式为:

首先直接使用其方法MySqlConnection,在出现错误提示后Alt+Enter显示可能修补的程序,然后选择安装MySQL.Data,VS2019会自动下载安装
在这里插入图片描述

当然也可以自行手动添加,在MySQL官网下载后,手动安装即可

二、定义连接连接数据库需要变量

首先创建一个类ConnDB作为封装数据库连接打开关闭使用,并定义需要使用的数据库连接变量:

  • 数据库地址:本机为localhost
  • 数据库端口号:一般默认为3306
  • 登录名:管理员用户为root
  • 登录密码:与用户对应的密码
  • 数据库名:你需要操作的数据库名称

具体的定义为:

		//定义连接地址、端口号、登录用户名、密码、数据库名
        private string server;
        private string port;
        private string user;
        private string password;
        private string datename;

三、构造函数获取对应参数

通过构造函数在类的实例化时获取需要输入的参数代码块为:

 		public ConnDB(string _server,string _port,string _user,string _password,string _datename)
        {
    
    
            this.server = _server;
            this.port = _port;
            this.user = _user;
            this.password = _password;
            this.datename = _datename;
        } 

四、连接并打开数据库

首先需要连接数据库,使用MySqlConnection()可以实现对于数据库的连接,其参数为连接之前定义的变量的字符串

使用string.Format()方法可以这些变量组成字符串:

		private MySqlConnection conn;
 		public MySqlConnection openDate()
        {
    
    
            try
            {
    
    
                string connStr = string.Format("server={0};port={1};user={2};password={3}; database={4};", server, port, user, password, datename);
                //连接数据库
                conn = new MySqlConnection(connStr);
                //打开
                conn.Open();
            }
            catch (Exception e)
            {
    
    
                Console.WriteLine("连接数据库错误,原因为:" + e.ToString());
            }
            return conn;            
        }

五,关闭数据库连接

定义一个函数来实现数据库的关闭功能:

public void closeDB(MySqlConnection conn)
        {
    
    
            //关闭数据库
            conn.Close();
        }

六、通过查询案例调用脚本

1,首先实例化该类,并通过类中的openDate()创建连接

2,然后使用MySqlCommand()方法来执行sql语句,完成查询

3,通过MySqlDataReader()对查询的结果进行数据处理

4,通过一定方式输出数据

	 	static void Main(string[] args)
        {
    
    
            //测试数据库
            ConnDB connDB = new ConnDB("localhost", "3306","root","***","**");
            MySqlConnection conn= connDB.openDate();
            string sqlStr = "select * from users";
            //执行sql语句,并返回结果
            MySqlCommand mycmd = new MySqlCommand(sqlStr,conn);
            //从返回结果中提取数据
            MySqlDataReader re = mycmd.ExecuteReader();
            if (re.HasRows)
            {
    
    
                while (re.Read())
                {
    
    
                    //读取第二列数据
                    Console.WriteLine(re[1]);
                }
            }
            connDB.closeDB();
        }

总结:

贴上数据库连接类完整源码:

using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;

namespace ServerDemo
{
    
    
    class ConnDB
    {
    
    
        //定义连接地址、端口号、登录用户名、密码、数据库名
        private string server;
        private string port;
        private string user;
        private string password;
        private string datename;

        private MySqlConnection conn;
        //构造函数接收参数
        public ConnDB(string _server,string _port,string _user,string _password,string _datename)
        {
    
    
            this.server = _server;
            this.port = _port;
            this.user = _user;
            this.password = _password;
            this.datename = _datename;
        } 
       // 
       /// <summary>
       ///  连接打开数据库
       /// </summary>
        public MySqlConnection openDate()
        {
    
    
            try
            {
    
    
                string connStr = string.Format("server={0};port={1};user={2};password={3}; database={4};", server, port, user, password, datename);
                //连接数据库
                conn = new MySqlConnection(connStr);
                //打开
                conn.Open();
            }
            catch (Exception e)
            {
    
    
                Console.WriteLine("连接数据库错误,原因为:" + e.ToString());
            }
            return conn;            
        }
        public void closeDB()
        {
    
    
            //关闭数据库
            conn.Close();
        }
    }
}

测试代码:

using System;
using MySql.Data.MySqlClient;

namespace ServerDemo
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            //测试数据库
            ConnDB connDB = new ConnDB("localhost", "3306","root","****","***");
            MySqlConnection conn= connDB.openDate();
            string sqlStr = "select * from users";
            //执行sql语句,并返回结果
            MySqlCommand mycmd = new MySqlCommand(sqlStr,conn);
            //从返回结果中提取数据
            MySqlDataReader re = mycmd.ExecuteReader();
            if (re.HasRows)
            {
    
    
                while (re.Read())
                {
    
    
                    //读取三列数据
                    Console.WriteLine(re[1]);
                }
            }
            

            connDB.closeDB();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/xinzhilinger/article/details/112917376