using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using MySql.Data.MySqlClient; public class MysqlManager { string ip = "127.0.0.1";//本机ip地址 string username = "root"; string password = "123456"; string dbName = "mrwang"; MySqlCommand command; MySqlConnection connect; public MysqlManager() { connect = new MySqlConnection(); connect.ConnectionString = "server=" + ip + ";user id=" + username + ";password=" + password + ";database=" + dbName; connect.Open(); Console.WriteLine("打开数据库成功"); command = connect.CreateCommand(); //Dictionary<string, string> skills = new Dictionary<string, string>(); //skills.Add("id ", "int primary key"); //skills.Add("name ", "varchar(10)"); //skills.Add("attack ", "int"); //skills.Add("ranges ", "float"); 根据子弹。创建表 //CreateTable(" Skill", skills); //InsertInto("skill"); //UpdateSet("skill"); //DeleteFrom("skill"); //SelectFrom("skill"); //CreateTable<UserData>(); //UserData user = new UserData(); //user.id = 1; //user.username = "1322283866@qq.com"; //user.nickname = "FollowYourHeart"; //user.password = "123456"; //InsertInto(user); //List<UserData> users = SelectFrom<UserData>(); UserData user = new UserData(); UpdateSet(user,user.id); } //创建表(带泛型) void CreateTable<T>() where T : class//where后面指泛型约束 { //反射 Type type = typeof(T); string tableName = type.Name;//通过反射拿到类名 System.Reflection.FieldInfo[] fileds = type.GetFields();//通过反射获取type类的字段 //FieldInfo.Name:字段名 //FieldInfo.FieldType:字段类型 string sql = "create table " + tableName + " ("; foreach (var item in fileds) { if (item.Name == "id") sql += item.Name + " " + CS2DB(item.FieldType) + " primary key auto_increment,";//主键 自动递增 else sql += item.Name + " " + CS2DB(item.FieldType) + ","; } sql = sql.TrimEnd(',') + ")";//删除最后的逗号 ExcuteSql(sql); } string CS2DB(Type type) { string dbType = ""; if (type.Name == "String") { dbType = "Text"; } else if (type.Name == "Int32") { dbType = "Int"; } else if (type.Name == "Float") { dbType = "Float"; } return dbType; } //创建表 void CreateTable(string tabelName, Dictionary<string, string> infos) { string sql = "create table " + tabelName + "("; foreach (var item in infos) { sql += item.Key + " " + item.Value + ","; } sql = sql.TrimEnd(',') + ");"; ExcuteSql(sql); } //增 void InsertInto(string tableName) { string sql = "insert into " + tableName + " " + "values(1,'大灾变',10,10)"; ExcuteSql(sql); } //增加(带泛型) void InsertInto<T>(T t) where T : class { //反射 Type type = typeof(T);//T可以拿到一个类的一个值,也可以设置值 string tableName = type.Name; System.Reflection.FieldInfo[] fileds = type.GetFields();//通过反射获取字段 string sql = "insert into " + tableName + " values("; foreach (System.Reflection.FieldInfo item in fileds)//FieldInfo:一个类的一个字段 { string value = item.GetValue(t).ToString();//通过反射得到t对象中某个字段的值 sql += "'" + value + "' ,"; } sql = sql.TrimEnd(',') + ")";//删除最后的逗号 ExcuteSql(sql); } //删 void DeleteFrom(string tableName) { string sql = "delete from " + tableName + " " + "where id=1 "; ExcuteSql(sql); } //删(带泛型) void DeleteFrom<T>(int id) { string tableName = typeof(T).Name; string sql = "delete from " + tableName + " " + "where id=1 " + id; ExcuteSql(sql); } //改 void UpdateSet(string tableName) { string sql = "update " + tableName + " " + "set name = '炎爆术' where id=1"; ExcuteSql(sql); } //改(带泛型) void UpdateSet<T>(T t, int id) { Type type = typeof(T);//T可以拿到一个类的一个值,也可以设置值 string tableName = typeof(T).Name; System.Reflection.FieldInfo[] fileds = type.GetFields();//通过反射获取字段 string sql = "update " + tableName + " " + "set name = '炎爆术' where id=1" + id; foreach (System.Reflection.FieldInfo item in fileds) { string value = item.GetValue(t).ToString();//通过反射得到t对象中某个字段的值 sql += "update" + value + " " + "set name = '火球术' where id=1" + id; } ExcuteSql(sql); } //查 void SelectFrom(string tableName) { string sql = "select * from " + tableName; command.CommandText = sql; MySqlDataReader reader = command.ExecuteReader();//以阅读器来查询 while (reader.Read()) { int count = reader.FieldCount; for (int i = 0; i < count; i++) { string key = reader.GetName(i);//列名 object value = reader.GetValue(i);//列值 Console.WriteLine(key + ":" + value); } } } //查询(带泛型) List<T> SelectFrom<T>() where T : class, new() { Type type = typeof(T); string tableName = type.Name;//得到泛型的名字 string sql = "select * from " + tableName; command.CommandText = sql; MySqlDataReader reader = command.ExecuteReader();//以阅读器来查询 List<T> array = new List<T>(); while (reader.Read()) { T t = new T(); int count = reader.FieldCount; for (int i = 0; i < count; i++) { string key = reader.GetName(i);//列名 object value = reader.GetValue(i);//列值 type.GetField(key).SetValue(t, value);//把t中的key字段,填充数据value Console.WriteLine(key + ":" + value); } array.Add(t); } return array; } void ExcuteSql(string sql) { Console.WriteLine("sql->" + sql); command.CommandText = sql; command.ExecuteNonQuery(); } }
C#向MySQL增删查改未完...
最新推荐文章于 2022-12-30 16:48:30 发布