Unity3D C# MySql 简单的登录验证

[csharp]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Net;  
  7. using System.Net.Sockets;  
  8. using System.Threading;  
  9.   
  10. public class Client : MonoBehaviour  
  11. {   
  12.     public string ipAddress = "127.0.0.1";  
  13.     IPAddress ip;  
  14.     Socket socket;  
  15.     // Use this for initialization  
  16.     void Start()  
  17.     {  
  18.         ip = IPAddress.Parse(ipAddress);  
  19.         socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  20.         socket.Connect(new IPEndPoint(ip, 8885));  
  21.     }  
  22.   
  23.     // Update is called once per frame  
  24.     void Update()  
  25.     {  
  26.   
  27.     }  
  28.   
  29.     void OnGUI()  
  30.     {  
  31.   
  32.         if (GUI.Button(new Rect(0, 20, 100, 20), "Connect"))  
  33.         {  
  34.             socket.Send(Encoding.ASCII.GetBytes("test,123456"));  
  35.         }  
  36.     }  
  37. }  

 

 

[csharp]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using ProtoBuf;  
  5. using System.Net;  
  6. using System.Data;  
  7. using System.Net.Sockets;  
  8. using System.Threading;  
  9. using MySql.Data.MySqlClient;  
  10.   
  11. namespace CenterServer  
  12. {  
  13.     class Program  
  14.     {  
  15.         private static byte[] result = new byte[1024];  
  16.         private static Socket serverSocket;  
  17.         private static int serverPort = 8885;  
  18.   
  19.   
  20.         static void Main(string[] args)  
  21.         {  
  22.   
  23.             IPAddress ip = IPAddress.Parse("127.0.0.1");  
  24.             serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  25.             serverSocket.Bind(new IPEndPoint(ip, serverPort));  
  26.             serverSocket.Listen(100);  
  27.             Console.WriteLine("启动监听{0}成功", serverSocket.LocalEndPoint.ToString());  
  28.             //通过Clientsoket发送数据    
  29.             Thread myThread = new Thread(ListenClientConnect);  
  30.             myThread.Start();  
  31.   
  32.             Console.ReadLine();  
  33.         }  
  34.   
  35.         /// <summary>    
  36.         /// 监听客户端连接    
  37.         /// </summary>    
  38.         private static void ListenClientConnect()  
  39.         {  
  40.             while (true)  
  41.             {  
  42.                 Socket clientSocket = serverSocket.Accept();  
  43.                 clientSocket.Send(Encoding.ASCII.GetBytes("Server Say Hello"));  
  44.                 Thread receiveThread = new Thread(ReceiveMessage);  
  45.                 receiveThread.Start(clientSocket);  
  46.             }  
  47.         }  
  48.   
  49.         /// <summary>    
  50.         /// 接收消息    
  51.         /// </summary>    
  52.         /// <param name="clientSocket"></param>    
  53.         private static void ReceiveMessage(object clientSocket)  
  54.         {  
  55.             Socket myClientSocket = (Socket)clientSocket;  
  56.   
  57.             while (true)  
  58.             {  
  59.                 try  
  60.                 {  
  61.                     //通过clientSocket接收数据    
  62.                     int receiveNumber = myClientSocket.Receive(result);  
  63.                     string[] message = Encoding.ASCII.GetString(result, 0, receiveNumber).Split(',');  
  64.                     string userName = message[0];  
  65.                     string passWord = message[1];  
  66.                     SQLHelper helper = new SQLHelper();  
  67.                     string sql = string.Format("SELECT * FROM test.account where account_name='" + userName + "'");  
  68.                     DataTable dt = helper.Selectinfo(sql);  
  69.                     if ((string)dt.Rows[0]["account_password"] == passWord)  
  70.                     {  
  71.                         Console.WriteLine(userName + " 登录验证成功");  
  72.                     }  
  73.                     else  
  74.                     {  
  75.                         Console.WriteLine(userName + " 登录验证失败");  
  76.                     }  
  77.                 }  
  78.                 catch (Exception ex)  
  79.                 {  
  80.                     Console.WriteLine(ex.Message);  
  81.                     myClientSocket.Shutdown(SocketShutdown.Both);  
  82.                     myClientSocket.Close();  
  83.                     break;  
  84.                 }  
  85.             }  
  86.         }  
  87.   
  88.         public class DBHelper  
  89.         {  
  90.             public MySqlConnection GetConn()  
  91.             {  
  92.                 MySqlConnection mysqlConn = new MySqlConnection("Database='" + "test" + "';Data Source='" + "localhost" + "';User Id='" + "root" + "';Password='" + "" + "'");    
  93.                 return mysqlConn;  
  94.             }  
  95.         }  
  96.   
  97.         public class SQLHelper : DBHelper  
  98.         {  
  99.             /// <summary>  
  100.             /// 查询操作  
  101.             /// </summary>  
  102.             /// <param name="sql"></param>  
  103.             /// <returns></returns>  
  104.             public DataTable Selectinfo(string sql)  
  105.             {  
  106.                 MySqlConnection mysqlconn = null;  
  107.                 MySqlDataAdapter sda = null;  
  108.                 DataTable dt = null;  
  109.                 try  
  110.                 {  
  111.                     mysqlconn = base.GetConn();  
  112.   
  113.                     sda = new MySqlDataAdapter(sql, mysqlconn);  
  114.                     dt = new DataTable();  
  115.                     sda.Fill(dt);  
  116.   
  117.                     return dt;  
  118.                 }  
  119.                 catch (Exception)  
  120.                 {  
  121.                     throw;  
  122.                 }  
  123.   
  124.             }  
  125.   
  126.             /// <summary>  
  127.             /// 增删改操作  
  128.             /// </summary>  
  129.             /// <param name="sql">sql语句</param>  
  130.             /// <returns>执行后的条数</returns>  
  131.             public int AddDelUpdate(string sql)  
  132.             {  
  133.   
  134.                 MySqlConnection conn = null;  
  135.                 MySqlCommand cmd = null;  
  136.   
  137.                 try  
  138.                 {  
  139.                     conn = base.GetConn();  
  140.                     conn.Open();  
  141.                     cmd = new MySqlCommand(sql, conn);  
  142.                     int i = cmd.ExecuteNonQuery();  
  143.                     conn.Close();  
  144.                     return i;  
  145.                 }  
  146.                 catch (Exception)  
  147.                 {  
  148.   
  149.                     throw;  
  150.                 }  
  151.                   
  152.             }  
  153.         }  
  154.     }  
  155. }  


 

 
0
0

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值