日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql雨凇_Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) | 雨松MOMO程序研究院...

發布時間:2025/4/5 数据库 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql雨凇_Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) | 雨松MOMO程序研究院... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

using UnityEngine;

using System;

using System.Data;

using System.Collections;

using MySql.Data.MySqlClient;

using MySql.Data;

using System.IO;

public class SqlAccess

{

public static MySqlConnection dbConnection;

//如果只是在本地的話,寫localhost就可以。

// static string host = "localhost";

//如果是局域網,那么寫上本機的局域網IP

static string host = "192.168.1.106";

static string id = "root";

static string pwd = "1234";

static string database = "xuanyusong";

public SqlAccess()

{

OpenSql();

}

public static void OpenSql()

{

try

{

string connectionString = string.Format("Server = {0};port={4};Database = {1}; User ID = {2}; Password = {3};",host,database,id,pwd,"3306");

dbConnection = new MySqlConnection(connectionString);

dbConnection.Open();

}catch (Exception e)

{

throw new Exception("服務器連接失敗,請重新檢查是否打開MySql服務。" + e.Message.ToString());

}

}

public DataSet CreateTable (string name, string[] col, string[] colType)

{

if (col.Length != colType.Length)

{

throw new Exception ("columns.Length != colType.Length");

}

string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];

for (int i = 1; i < col.Length; ++i) {

query += ", " + col[i] + " " + colType[i];

}

query += ")";

return??ExecuteQuery(query);

}

public DataSet CreateTableAutoID (string name, string[] col, string[] colType)

{

if (col.Length != colType.Length)

{

throw new Exception ("columns.Length != colType.Length");

}

string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0] +??" NOT NULL AUTO_INCREMENT";

for (int i = 1; i < col.Length; ++i) {

query += ", " + col[i] + " " + colType[i];

}

query += ", PRIMARY KEY ("+ col[0] +")" + ")";

Debug.Log(query);

return??ExecuteQuery(query);

}

//插入一條數據,包括所有,不適用自動累加ID。

public DataSet InsertInto (string tableName, string[] values)

{

string query = "INSERT INTO " + tableName + " VALUES (" + "'"+ values[0]+ "'";

for (int i = 1; i < values.Length; ++i) {

query += ", " + "'"+values[i]+ "'";

}

query += ")";

Debug.Log(query);

return ExecuteQuery (query);

}

//插入部分ID

public DataSet InsertInto (string tableName, string[] col,string[] values)

{

if (col.Length != values.Length)

{

throw new Exception ("columns.Length != colType.Length");

}

string query = "INSERT INTO " + tableName + " (" + col[0];

for (int i = 1; i < col.Length; ++i)

{

query += ", "+col[i];

}

query += ") VALUES (" + "'"+ values[0]+ "'";

for (int i = 1; i < values.Length; ++i)

{

query += ", " + "'"+values[i]+ "'";

}

query += ")";

Debug.Log(query);

return ExecuteQuery (query);

}

public DataSet SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values)

{

if (col.Length != operation.Length || operation.Length != values.Length) {

throw new Exception ("col.Length != operation.Length != values.Length");

}

string query = "SELECT " + items[0];

for (int i = 1; i < items.Length; ++i) {

query += ", " + items[i];

}

query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";

for (int i = 1; i < col.Length; ++i) {

query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";

}

return ExecuteQuery (query);

}

public DataSet UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,string selectvalue)

{

string query = "UPDATE "+tableName+" SET "+cols[0]+" = "+colsvalues[0];

for (int i = 1; i < colsvalues.Length; ++i) {

query += ", " +cols[i]+" ="+ colsvalues[i];

}

query += " WHERE "+selectkey+" = "+selectvalue+" ";

return ExecuteQuery (query);

}

public DataSet Delete(string tableName,string []cols,string []colsvalues)

{

string query = "DELETE FROM "+tableName + " WHERE " +cols[0] +" = " + colsvalues[0];

for (int i = 1; i < colsvalues.Length; ++i)

{

query += " or " +cols[i]+" = "+ colsvalues[i];

}

Debug.Log(query);

return ExecuteQuery (query);

}

public??void Close()

{

if(dbConnection != null)

{

dbConnection.Close();

dbConnection.Dispose();

dbConnection = null;

}

}

public static DataSet ExecuteQuery(string sqlString)

{

if(dbConnection.State==ConnectionState.Open)

{

DataSet ds = new DataSet();

try

{

MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);

da.Fill(ds);

}

catch (Exception ee)

{

throw new Exception("SQL:" + sqlString + "/n" + ee.Message.ToString());

}

finally

{

}

return ds;

}

return null;

}

}

總結

以上是生活随笔為你收集整理的mysql雨凇_Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) | 雨松MOMO程序研究院...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。