一:下載安裝Mysql
鏈接:https://pan.baidu.com/s/14KxWgyUEHtzVpLlAiDa6ag
提取碼:ydkf
複製這段內容後打開百度網盤手機App,操作更方便哦
安裝過程自行百度
圖形化MySql操作工具Navicat Premium 12
鏈接:https://pan.baidu.com/s/1CKh0szB0w6EAQ6lkT8KUbg
提取碼:70cb
複製這段內容後打開百度網盤手機App,操作更方便哦
破解Navicat Premium 12 補丁
鏈接:https://pan.baidu.com/s/1XynAl2TAb6JPiIe6Ef8jyw
提取碼:ngar
複製這段內容後打開百度網盤手機App,操作更方便哦
選擇正確的文件夾版本複製到Navicat Premium 12安裝的根目錄即可破解
準備就緒後建立數據庫
在數據庫中建立表格
安成後保存 完成數據庫的建立
二:連接數據庫
提前需要進行如下操作:
右擊項目選擇管理NuGet程序包…
然後搜索Mysql
安裝上圖程序包
打開VS
添加類MysqlAssess,其中包括登錄、註冊以及生成ID(按時間生成)的相關函數
using System.Collections;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System.Data;
using System;
namespace AR_Server
{
public class MySqlAccess
{
public static String hpname;
//連接類對象
private static MySqlConnection mySqlConnection;
//IP地址
private static string host;
//端口號
private static string port;
//用戶名
private static string userName;
//密碼
private static string password;
//數據庫名稱
private static string databaseName;
//string sqlCon = "server=localhost;user id=root;password=WANGshuai123...;data=userTable";
/// <summary>
/// 構造方法
/// </summary>
/// <param name="_host">ip地址</param>
/// <param name="_userName">用戶名</param>
/// <param name="_password">密碼</param>
/// <param name="_databaseName">數據庫名稱</param>
public MySqlAccess(string _host, string _port, string _userName, string _password, string _databaseName)
{
host = _host;
port = _port;
userName = _userName;
password = _password;
databaseName = _databaseName;
}
/// <summary>
/// 打開數據庫
/// </summary>
public bool OpenSql(string S_name, string S_password)
{
bool a = false;
try
{
string mySqlString = string.Format("Database={0};Data Source={1};User Id={2};Password={3};port={4}", databaseName, host, userName, password, port);
MySqlConnection conn = new MySqlConnection(mySqlString);
//if(mySqlConnection.State == ConnectionState.Closed)
conn.Open();
MySqlCommand cma = new MySqlCommand();
cma.Connection = conn;
cma.CommandText = "select * from user where '" + S_name + "'";
MySqlDataReader reader = cma.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
String res = reader.GetString(0);
}
if (S_name == reader.GetString(0) && S_password == reader.GetString(2))
{
a = true;
}
hpname = reader.GetString(3);
return a;
}
catch (Exception e)
{
throw new Exception("服務器連接失敗,請重新檢查MySql服務是否打開。" + e.Message.ToString());
}
return a;
}
public bool OpenSql1(string S_name, string S_password, string S_user_name)//註冊賬戶函數
{
bool a = false;
try
{
string mySqlString = string.Format("Database={0};Data Source={1};User Id={2};Password={3};port={4}", databaseName, host, userName, password, port);
MySqlConnection conn = new MySqlConnection(mySqlString);
//if(mySqlConnection.State == ConnectionState.Closed)
conn.Open();
MySqlCommand cma = new MySqlCommand();
cma.Connection = conn;
cma.CommandText = "select count(id) from user where tet = '" + S_name + "'";
MySqlDataReader reader = cma.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
String res = reader.GetString(0);
}
Console.WriteLine(reader.GetInt32(0));
if (reader.GetInt32(0) == 0)//判斷賬號是否創建
{
a = insertData(S_name, S_password, S_user_name);
return a;
}
else
{
a = false;
Console.WriteLine("該賬號已註冊!!!");
return false;
}
}
catch (Exception e)
{
throw new Exception("服務器連接失敗,請重新檢查MySql服務是否打開。" + e.Message.ToString());
}
return a;
}
bool insertData(string s_name, string s_password, string S_user_name)//插入數據
{
bool a = true;
string mySqlString = string.Format("Database={0};Data Source={1};User Id={2};Password={3};port={4}", databaseName, host, userName, password, port);
MySqlConnection conn = new MySqlConnection(mySqlString);
//if(mySqlConnection.State == ConnectionState.Closed)
conn.Open();
MySqlCommand cma = new MySqlCommand();
cma.Connection = conn;
string id = get_id();
Console.WriteLine(S_user_name);
cma.CommandText = "insert into user values('" + id + "','" + s_name + "','" + s_password + "','" + S_user_name + "')";
cma.ExecuteNonQuery();
return a;
}
string get_id()//生成id
{
string a = System.DateTime.Now.ToString();
a = a.Replace("/", "");
a = a.Replace(" ", "");
a = a.Replace(":", "");
Console.WriteLine(a);
return a;
}
/// <summary>
/// 關閉數據庫
/// </summary>
public void CloseSql()//關閉服務器
{
if (mySqlConnection != null)
{
mySqlConnection.Close();
mySqlConnection.Dispose();
mySqlConnection = null;
}
}
/// <summary>
/// 查詢數據
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="items">要查詢的列</param>
/// <param name="whereColumnName">查詢的條件列</param>
/// <param name="operation">條件操作符</param>
/// <param name="value">條件的值</param>
/// <returns></returns>
public DataSet Select(string tableName, string[] items, string[] whereColumnName,
string[] operation, string[] value)
{
if (whereColumnName.Length != operation.Length || operation.Length != value.Length)
{
throw new Exception("輸入不正確:" + "要查詢的條件、條件操作符、條件值 的數量不一致!");
}
string query = "Select " + items[0];
for (int i = 1; i < items.Length; i++)
{
query += "," + items[i];
}
query += " FROM " + tableName + " WHERE " + whereColumnName[0] + " " + operation[0] + " '" + value[0] + "'";
for (int i = 1; i < whereColumnName.Length; i++)
{
query += " and " + whereColumnName[i] + " " + operation[i] + " '" + value[i] + "'";
}
return QuerySet(query);
}
/// <summary>
/// 執行SQL語句
/// </summary>
/// <param name="sqlString">sql語句</param>
/// <returns></returns>
private DataSet QuerySet(string sqlString)
{
if (mySqlConnection.State == ConnectionState.Open)
{
DataSet ds = new DataSet();
try
{
MySqlDataAdapter mySqlAdapter = new MySqlDataAdapter(sqlString, mySqlConnection);
mySqlAdapter.Fill(ds);
}
catch (Exception e)
{
throw new Exception("SQL:" + sqlString + "/n" + e.Message.ToString());
}
finally
{
}
return ds;
}
return null;
}
public bool S_user_name(string S_user_name)//判斷暱稱是否合法
{
bool a = false;
try
{
string mySqlString = string.Format("Database={0};Data Source={1};User Id={2};Password={3};port={4}", databaseName, host, userName, password, port);
MySqlConnection conn = new MySqlConnection(mySqlString);
//if(mySqlConnection.State == ConnectionState.Closed)
conn.Open();
MySqlCommand cma = new MySqlCommand();
cma.Connection = conn;
cma.CommandText = "select count(id) from user where user_name = '" + S_user_name + "'";
MySqlDataReader reader = cma.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
String res = reader.GetString(0);
}
if (reader.GetInt32(0) == 0)//判斷暱稱是否合法
{
a = true;
}
else
{
a = false;
}
}
catch (Exception e)
{
throw new Exception("服務器連接失敗,請重新檢查MySql服務是否打開。" + e.Message.ToString());
}
return a;
}
}
}
添加類ReginsterClass,註冊類
using System;
using System.Collections;
using System.Collections.Generic;
namespace AR_Server
{
public class ReginsterClass
{
MySqlAccess mysql = new MySqlAccess("127.0.0.1", "3306", "root", "123456", "userapp");
string S_name = null;
string S_passworld = null;
string S_repassword = null;
string S_user_name = null;
bool a = false;
public ReginsterClass(string a, string b, string c,string d)
{
S_name = a;
S_passworld = b;
S_repassword = c;
S_user_name = d;
}
public void Finish_Register()//向服務器提交註冊信息
{
bool a = mysql.OpenSql1(S_name, S_passworld, S_user_name);
mysql.CloseSql();
if (a == true)
{
Console.WriteLine("註冊成功!請登錄...");
}
else
{
Console.WriteLine("註冊失敗,請重試......");
}
}
}
}
添加登錄類
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
namespace AR_Server
{
public class LogionClass
{
MySqlAccess mysql = new MySqlAccess("127.0.0.1", "3306", "root", "123456", "userapp");
private string S_name;
private string S_password;
// Start is called before the first frame update
public LogionClass(string a, string b)
{
S_name = a;
S_password = b;
}
public void To_Login()//登錄
{
bool a = mysql.OpenSql(S_name, S_password);
mysql.CloseSql();
/*Debug.Log(user_name + password);*/
if (a == true)
{
Console.WriteLine("登陸成功!);
}
else
{
Console.WriteLine("賬號或密碼輸入錯誤!請重新輸入");
}
}
}
}
三:測試
新建主類MainClass
註冊測試
using System;
using System.Collections.Generic;
using System.Text;
namespace AR_Server
{
class MainClass
{
public static void Main(string[] arg)
{
ReginsterClass a = new ReginsterClass("223332", "223332", "223332", "cccd");
a.Finish_Register();
}
}
}
登錄測試:
using System;
using System.Collections.Generic;
using System.Text;
namespace AR_Server
{
class MainClass
{
public static void Main(string[] arg)
{
LogionClass a = new LogionClass("17547625284", "wmy.521");
a.To_Login();
}
}
}
連接本地數據庫ip爲127.0.0.1或loccalhost 端口號爲3306
至此用戶登錄及註冊程序模板寫好…