MySql数据库在C#中的一些应用

前提:需要安装MySql的Windows环境以及下载MySql的.Net连接文件,即相关.dll文件
本文使用的是8.0.17版本的连接文件,支持.net 4.5.2以上环境编译.
GitHub地址为:https://github.com/CamelWH/MySqlForCSharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace MySqlForCSharp
{
    public class MySqlHelper
    {
        public static MySqlConnection connect;
        public static MySqlCommand command;
        public static MySqlDataReader dataReader;
        /// 
        /// 连接数据库
        /// MySqlHelper.ConnectDataBase("root", "root", "localhost", "skypath", "3306");
        /// Server=xxx;UserId=yyy;Password=zzz;Database=dbdb
        /// 
        /// 用户名
        /// 用户密码
        /// 数据库地址
        /// 数据库名称
        /// 端口号
        public static void ConnectDataBase(string user, string password, string server, string database, string port)
        {
            try
            {
                if (connect != null)
                    connect.Close();
                string str = "user=" + user + ";password=" + password + ";server=" + server + ";port=" + port + ";database=" + database + ";";
                Console.WriteLine(str);
                connect = new MySqlConnection(str);
                connect.Open();
                command = connect.CreateCommand();
                Console.WriteLine("连接数据库成功!");
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e);
            }
        }
        /// 
        /// 关闭数据库
        /// 
        public static void CloseConnect()
        {
            //销毁Command
            if (command != null)
            {
                command.Cancel();
            }
            command = null;

            //销毁Reader
            //if (dataReader != null)
            //{
            //    dataReader.Close();
            //}
            //dataReader = null;

            //销毁Connection
            if (connect != null)
            {
                connect.Close();
            }
            connect = null;
        }
        /// 
        /// 向数据库添加一个表,列类型为string
        /// Dictionary dict = new Dictionary();
        /// dict.Add("user", "text");
        /// MySqlHelper.CreateTable("testTable", dict);
        /// 
        /// 表名称
        /// 列名称
        public static void CreateTable(string tableName, Dictionary colNameType)
        {
            string content = string.Empty;
            foreach (var item in colNameType)
            {
                content += item.Key + " " + item.Value + ",";
            }
            content = content.Remove(content.Length - 1);
            string opcode = "create table " + tableName + "(" + content + ")" + ";";
            Console.WriteLine(opcode);
            ExecuteQuery(opcode);
        }
        /// 
        /// 删除一个表
        /// 
        /// 
        public static void DeleteTable(string tableName)
        {
            string opcode = "drop table " + tableName + ";";
            ExecuteQuery(opcode);
        }
        /// 
        /// 向一个表中添加数据
        /// MySqlHelper.AddData("testTable", new string[] { "user", "id" }, new string[] { "'abc'", "123" });
        /// text型:需要添加''
        /// 
        public static void AddData(string tableName, string[] colName, string[] values)
        {
            string content = "(" + string.Join(",", colName) + ")";
            string value = "(" + string.Join(",", values) + ")";
            string opcode = "insert into " + tableName + content + "values" + value;
            Console.WriteLine(opcode);
            ExecuteQuery(opcode);
        }
        /// 
        /// 更新表中的某个数据
        /// MySqlHelper.UpdateData("testTable", "user", "'仙'", "id", "=", "1");
        /// 
        public static void UpdateData(string tableName, string colName, string value, string key,string op,string keyValue)
        {
            string queryString = string.Format("update {0} set {1}={2} where {3}{4}{5};", tableName, colName, value, key, op, keyValue);
            Console.WriteLine(queryString);
            ExecuteQuery(queryString);
        }
        /// 
        /// 删除表中某个数据
        /// MySqlHelper.DeleteData("testTable", "user", "=", "'人'");
        /// 
        public static void DeleteData(string tableName, string colName, string operation, string colValue)
        {
            string queryString = "delete from " + tableName + " where " + colName + " " + operation+ colValue + ";";
            Console.WriteLine(queryString);
            ExecuteQuery(queryString);
        }
        /// 
        /// 读取整张表
        /// 
        /// 
        /// 
        public static Table ReadTable(string tableName)
        {
            string queryString = "select*from " + tableName;
            Table table = new Table();
            table.Change(tableName);
            int lineNum = 1;
            using (command = new MySqlCommand(queryString, connect))
            {
                try
                {
                    dataReader=command.ExecuteReader();
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        table.colNames.Add(dataReader.GetName(i));
                    }
                    while (dataReader.Read())
                    {
                        for (int i = 0; i < dataReader.FieldCount; i++)
                        {
                            Element element = new Element();
                            element.Change(lineNum, dataReader.GetName(i), dataReader[dataReader.GetName(i)]);
                            Console.WriteLine(element);
                            table.AddElement(element);
                        }
                        lineNum++;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            Console.WriteLine(table);
            return table;
        }
        /// 
        /// 执行指令
        /// 
        private static void ExecuteQuery(string queryString)
        {
            using (command = new MySqlCommand(queryString, connect))
            {
                try
                {
                    command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
        /// 
        /// 用户密码验证
        /// 
        public static bool VerifyUser(string userName, string password)
        {
            string sqlstr = "select * from czhenya001 where username = @para1 and password = @para2";
            command = new MySqlCommand(sqlstr, connect);
            command.Parameters.AddWithValue("para1", userName);
            command.Parameters.AddWithValue("para2", password);
            MySqlDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                return true;
            }
            return false;
        }
    }
    /// 
    /// 自制的数据表
    /// 
    public class Table
    {
        /// 
        /// 表名
        /// 
        public string tableName;
        /// 
        /// 列名
        /// 
        public List colNames;
        /// 
        /// 该表的所有元素
        /// 
        public Dictionary> content;
        /// 
        /// 添加元素
        /// 
        /// 
        /// 
        public void AddElement(Element e)
        {
            if (!content.ContainsKey(e.lineNum))
            {
                content.Add(e.lineNum, new List());
            }
            content[e.lineNum].Add(e);
        }
        public Table()
        {
        }
        public void Change(string tableName)
        {
            this.tableName = tableName;
            this.colNames = new List();
            this.content = new Dictionary>();
        }
        public override string ToString()
        {
            string str="表名:"+tableName+"\n"+"列名:"+string.Join(",",colNames);
            foreach (var item in content)
            {
                str += "\n" + string.Join("----", item.Value);
            }
            return str;
        }
    }
    /// 
    /// 表中一个元素
    /// 
    public class Element
    {
        /// 
        /// 该元素的的内容
        /// 
        public object content;
        /// 
        /// 该元素的行号
        /// 
        public int lineNum;
        /// 
        /// 该元素的列名
        /// 
        public string colName;
        public Element()
        {
        }
        public void Change(int lineNum, string colName, object content)
        {
            this.lineNum = lineNum;
            this.colName = colName;
            this.content = content;
        }
        public override string ToString()
        {
            return string.Format("行号:{0};列名:{1};内容:{2};类型:{3}", lineNum, colName, content,content.GetType());
        }
    }
}

你可能感兴趣的:(数据库相关)