using UnityEngine;
using System;
using System.Data;
using MySql.Data.MySqlClient;
using System.IO;using UnityEngine.UI;
public class SqlAccess
{public static MySqlConnection dbConnection;//如果只是在本地的话,写localhost就可以。//static string host = "localhost"; //如果是局域网,那么写上本机的局域网IP//static string host = "服务器地址";//static string database = "数据库名称";//static string id = "用户名";//static string pwd = "密码";static string host = "服务器地址";static string database = "数据库名称";static string id = "用户名";static string pwd = "密码";public SqlAccess(){OpenSql();}public static void OpenSql(){try{#region 旧//var settingsPath=Application.dataPath + "/settings.cfg";//if (File.Exists(settingsPath)) {// StreamReader textReader=new StreamReader ( settingsPath , System.Text.Encoding.ASCII );// host = textReader.ReadLine ( );// GameObject.Find("Text1").GetComponent().text=host.ToString();// }//GameObject.Find("Text").GetComponent().text=host.ToString();#endregionstring connectionString = string.Format("Server = {0};Database = {1}; User ID = {2}; Password = {3};port={4};", 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);}/// /// 返回表的查询结果/// /// The all./// Name.public DataSet SelectAll(string Name){string query = "select * from " + " " + Name;return ExecuteQuery(query);}/// /// 条件查找/// /// The where./// 表名/// 选择的字段/// 查找根据的字段/// 查找根据的运算符/// 查找根据的值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);}/// /// 更新表信息/// /// The into./// 表名/// 更新的字段/// 更新字段的值/// 查找根据的字段/// 字段的值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 UpdateInto1(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 DataSet ExistOrNot(string sql){if (dbConnection.State == ConnectionState.Open){//Debug.Log (sql);DataSet ds = new DataSet(); //表的集合try{MySqlDataAdapter da = new MySqlDataAdapter(sql, dbConnection);da.Fill(ds);}catch (Exception ee){throw new Exception("SQL:" + sql + "/n" + ee.Message.ToString());}finally{}return ds;}return null;}/// /// 返回检索结果/// /// The query./// Sql string.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;}/// /// 读取数据集/// /// Ds.public void ReadDs(DataSet ds){if (ds != null){DataTable user = ds.Tables[0];foreach (DataRow row in user.Rows){foreach (DataColumn colum in user.Columns){//Debug.Log (row[colum]);}}}}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!