博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySql增删查改
阅读量:5875 次
发布时间:2019-06-19

本文共 8730 字,大约阅读时间需要 29 分钟。

using MySql.Data.MySqlClient;using System;using System.Collections.Generic;using System.Data;using UnityEngine;public class SqlConnectJson{    public string Server;    public string port;    public string Database;    public string UserID;    public string Password;    public string ConnectionTimeOut;}public class SQLData{
private static SQLData instance; private SQLData() { } public static SQLData MySqlInstance { get { if (null == instance) { instance = new SQLData(); } return instance; } } //连接部分 private string connString = ""; public string ConnString { set { connString = value; } get { return connString; } } private MySqlConnection connection = null; //获取连接数据库对象 private MySqlConnection Connection { get { try { if (connection == null) { Debug.Log(connString); connection = new MySqlConnection(connString); connection.Open(); } else if (connection.State == ConnectionState.Closed) { connection = new MySqlConnection(connString); connection.Open(); } else if (connection.State == ConnectionState.Broken) { connection.Close(); connection.Open(); } return connection; } catch (Exception e) { throw new Exception("Database connection failed. Please Check MySql Service!" + "\n" + e.Message.ToString()); } } } public bool InsetInfo(string tableName, string[] col, string[] values) { if (col.Length != values.Length) { throw new Exception("coluns.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 += ")"; return executeSQL(query); } public bool InsetInfo(string tableName, string col, string values) { string query = "insert into " + tableName + " (" + col + ") values (" + values + ")"; return executeSQL(query); } //更新方法 public bool UpdateInfo(string tableName, string col, string value, string whereName, string whereValue) { string query = "update " + tableName + " set " + col + " = " + "'" + value + "' where " + whereName + " = '" + whereValue + "'"; Debug.Log(query); return executeSQL(query); } public bool UpdateInfo(string tableName, string[] col, string[] value, string whereName, string whereValue) { if (col.Length != value.Length) { throw new Exception("coluns.Length != colType.Length"); } string query = "update " + tableName + " set " + col[0] + " = " + "'" + value[0] + "'"; for (int i = 1; i < col.Length; i++) { query += "," + col[i] + " = " + "'" + value[i] + "'"; } query += "where " + whereName + " = " + "'" + whereValue + "'"; return executeSQL(query); } /// ///精细查找方法 /// /// 目标名 /// 查询的表名 /// 依据名 /// 依据 public List
LocalSelectInfo(string Target, string TableName, string Basis, string BasInfo) { MySqlDataAdapter SqlData = null; List
mList = new List
(); string Que = string.Format("SELECT {0} FROM {1} WHERE {2}='{3}'", Target, TableName, Basis, BasInfo); // Debug.Log(Que); SqlData = new MySqlDataAdapter(Que, Connection); string Data = ""; //实例化数据集,并写入查询到的数据 DataSet ds = new DataSet(); SqlData.Fill(ds); //按行和列打印出数据 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { Data += ds.Tables[0].Rows[i][j].ToString().Trim() + "@"; if (j == ds.Tables[0].Columns.Count - 1) { string[] mData; mData = Data.Split('@'); string Tmp = ""; for (int z = 0; z < mData.Length; z++) { if (mData[z] != "") { if (z == mData.Length - 2) { Tmp += mData[z]; mList.Add(mData[z]); } else { Tmp += mData[z] + "@"; mList.Add(mData[z]); } } } Data = ""; } } } return mList; } ///
///精细查找方法 /// ///
目标名 ///
查询的表名 public List
LocalSelectInfo(string Target, string TableName) { MySqlDataAdapter SqlData = null; List
mList = new List
(); string Que = string.Format("SELECT {0} FROM {1}", Target, TableName); // Debug.Log(Que); SqlData = new MySqlDataAdapter(Que, Connection); string Data = ""; //实例化数据集,并写入查询到的数据 DataSet ds = new DataSet(); SqlData.Fill(ds); //按行和列打印出数据 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { Data += ds.Tables[0].Rows[i][j].ToString().Trim() + "@"; if (j == ds.Tables[0].Columns.Count - 1) { string[] mData; mData = Data.Split('@'); string Tmp = ""; for (int z = 0; z < mData.Length; z++) { if (mData[z] != "") { if (z == mData.Length - 2) { Tmp += mData[z]; mList.Add(mData[z]); } else { Tmp += mData[z] + "@"; mList.Add(mData[z]); } } } Data = ""; } } } return mList; } //从表中随机固定数目的数据 public void SetectInfoRandom(int Num, string TableName) { MySqlDataAdapter SqlData = null; string Que = string.Format("select* from {1} order by rand() limit {0}", Num, TableName); // Debug.Log(Que); SqlData = new MySqlDataAdapter(Que, Connection); string Data = ""; //实例化数据集,并写入查询到的数据 DataSet ds = new DataSet(); SqlData.Fill(ds); //按行和列打印出数据 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { Data += ds.Tables[0].Rows[i][j].ToString().Trim() + "@"; if (j == ds.Tables[0].Columns.Count - 1) { string[] mData; mData = Data.Split('@'); string Tmp = ""; for (int z = 0; z < mData.Length; z++) { if (mData[z] != "") { if (z == mData.Length - 2) { Tmp += mData[z]; } else { Tmp += mData[z] + "@"; } } } Debug.Log(Tmp); Data = ""; } } } } ///
/// 随机指定类型题目 /// ///
目标名 ///
查询的表名 ///
依据名 ///
依据 ///
数目 public List
LocalSelectInfo(string Target, string TableName, string Basis, string BasInfo, int Num) { List
mList = new List
(); MySqlDataAdapter SqlData = null; string Que = string.Format("SELECT {0} FROM {1} WHERE {2}='{3}' order by rand() limit {4}", Target, TableName, Basis, BasInfo, Num); // Debug.Log(Que); SqlData = new MySqlDataAdapter(Que, Connection); string Data = ""; //实例化数据集,并写入查询到的数据 DataSet ds = new DataSet(); SqlData.Fill(ds); //按行和列打印出数据 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { Data += ds.Tables[0].Rows[i][j].ToString().Trim() + "@"; if (j == ds.Tables[0].Columns.Count - 1) { string[] mData; mData = Data.Split('@'); string Tmp = ""; for (int z = 0; z < mData.Length; z++) { if (mData[z] != "") { if (z == mData.Length - 2) { Tmp += mData[z]; mList.Add(mData[z]); } else { Tmp += mData[z] + "@"; mList.Add(mData[z]); } } } Data = ""; } } } return mList; } //删除方法 public bool Delete(string tableName, string col, string value) { string query = "delete from " + tableName + " where " + col + " = '" + value + "'"; return executeSQL(query); } //创建MySqlCommand对象 MySql语句不带参数 public MySqlCommand Command(string strMySql) { try { using (MySqlCommand cmd = new MySqlCommand(strMySql, Connection)) { return cmd; } } catch { return null; } } //更新、添加、删除操作是否成功 public bool executeSQL(string strMySql) { int rows = executeNonQuery(strMySql); if (rows > 0) { return true; } else { return false; } } //返回受影响的行数 MySql语句不带参数(更新 添加 删除) public int executeNonQuery(string strMySql) { try { MySqlCommand cmd = Command(strMySql); return cmd.ExecuteNonQuery(); } catch { return 0; } }}

 

转载于:https://www.cnblogs.com/SevenPixels/p/10911812.html

你可能感兴趣的文章
深入理解javascript对象系列第一篇——初识对象
查看>>
Ubuntu16.04下搭建Go语言环境
查看>>
eclipse调试(debug)的时候,出现Source not found,Edit Source Lookup Path,一闪而过
查看>>
Html5视频播放器-VideoJS+Audio标签实现视频,音频及字幕同步播放
查看>>
Kafka消息模拟器
查看>>
Linux常用基本命令(cat)
查看>>
Redis_master-slave模式
查看>>
qemu安装
查看>>
多媒体开发之rtmp---rtmp client 端的实现
查看>>
3.使用Maven构建Web项目
查看>>
产生6位组合随机数
查看>>
Android录制视频报错setVideoSize called in a invalid state 1
查看>>
angularjs ui-router
查看>>
python nltk 入门demo
查看>>
Android动态权限处理
查看>>
MySQL InnoDB 存储引擎探秘
查看>>
Python学习之数据类型
查看>>
从闭包函数的变量自增的角度 - 解析js垃圾回收机制
查看>>
java解析json字符串详解(两种方法)
查看>>
Java虚拟机 —— 垃圾回收机制
查看>>