using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MySql.Data.MySqlClient;
using System.Data;
using System;
public class getGameUserAccount : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
mySqlCon();
}
// Update is called once per frame
void Update()
{
}
public void mySqlCon()
{
//數據庫登錄數據
string conStr = "server=localhost;User Id = root;password=123456;Database=gamerdata;charset=utf8";
//建立連接
//實例化的同時調用MySqlConnection,傳入參數
//這里的傳入參數個人認為是CMD里面的直接輸入了,string格式直接類似手敲到cmd里面
MySqlConnection myCon = new MySqlConnection(conStr);
//打開連接
myCon.Open();
//插入數據,其中useraccount為表名,括號內為表的格式
/*
//此處注釋是因為不能添加相同主鍵的值
MySqlCommand myCmd = new MySqlCommand("insert into useraccount(id,nickname,password) values (4,'list','testList')", myCon);
if (myCmd.ExecuteNonQuery() > 0)
{
Debug.Log("Query Success!");
}
*/
//查詢數據
string selStr = "select * from useraccount";
MySqlCommand mySelect = new MySqlCommand(selStr, myCon);
DataSet ds = new DataSet();
try
{
MySqlDataAdapter da = new MySqlDataAdapter(selStr, myCon);
da.Fill(ds);
Debug.Log(ds.Tables[0].Rows[0][0]);
Debug.Log(ds.Tables[0].Rows[0][1]);
Debug.Log(ds.Tables[0].Rows[0][2]);
Debug.Log(ds.Tables[0].Rows[0][3]);
//Table[0].Rows[0][0]
Debug.Log("Query Success!");
}
catch (Exception e)
{
throw new Exception("SQL:" + selStr + "\n" + e.Message.ToString());
}
myCon.Close();
}
}
到此這篇關于Unity連接MySQL時讀取表格的方式的文章就介紹到這了,更多相關Unity連接MySQL讀取表格內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!