mysql 动态增加列,查找表中有多少列,具体什么列。 通过JSON生成mysql表 支持子JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { using (testEntities entity = new testEntities()) { var tableName = "test2" ; var json = "{\"name\":\"hello\", \"age\":1, \"createtime\":\"2012-04-23T18:25:43.511Z\", object:{\"column1\":\"test\", \"column2\":\"test\"}}" ; var jsonEntity = JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(json); var tableList = entity.Database.SqlQuery<MysqlTableSchema>( "show tables" ).ToListAsync(); tableList.Wait(); var isExistsTable = tableList.Result.Where(r => r.tables_in_test.ToLower() == tableName.ToLower()).Count() > 0; if (!isExistsTable) { entity.Database.ExecuteSqlCommand( "CREATE TABLE `" + tableName + "` (`id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));" ); } CreateColumns(tableName, jsonEntity); } } private static void CreateColumns( string tableName, JObject jsonEntity, string columPrefix = "" ) { using (testEntities entity = new testEntities()) { var columList = entity.Database.SqlQuery<MysqlColumSchema>( "DESCRIBE " + tableName).ToListAsync(); columList.Wait(); foreach ( var item in jsonEntity.Properties()) { var columName = string .IsNullOrEmpty(columPrefix) ? item.Name.ToLower() : columPrefix + "_" + item.Name.ToLower(); var isExists = columList.Result.Where(e => e.field.ToLower().Equals(columName)).ToList().Count() > 0; if (!isExists) { var mysqlType = "" ; var index = "" ; switch (item.Value.Type) { case Newtonsoft.Json.Linq.JTokenType.Object: CreateColumns(tableName, item.Value.ToObject<JObject>(), columPrefix = item.Name.ToLower()); continue ; case Newtonsoft.Json.Linq.JTokenType.Boolean: mysqlType = "nvarchar(1000) " ; index = "ALTER TABLE " + tableName + " add index INDEX_" +columName+ " (" +columName+ ");" ; break ; case Newtonsoft.Json.Linq.JTokenType.Bytes: break ; case Newtonsoft.Json.Linq.JTokenType.Comment: mysqlType = "nvarchar(1000) " ; index = "ALTER TABLE " + tableName + " add index INDEX_" +columName+ " (" +columName+ ");" ; break ; case Newtonsoft.Json.Linq.JTokenType.Date: mysqlType = "datetime NULL DEFAULT CURRENT_TIMESTAMP" ; index = "ALTER TABLE " + tableName + " add index INDEX_" +columName+ " (" +columName+ ");" ; break ; case Newtonsoft.Json.Linq.JTokenType.Float: mysqlType = "decimal(6,2) NULL DEFAULT '0.00'" ; index = "ALTER TABLE " + tableName + " add index INDEX_" +columName+ " (" +columName+ ");" ; break ; case Newtonsoft.Json.Linq.JTokenType.Guid: mysqlType = "nvarchar(45) " ; index = "ALTER TABLE " + tableName + " add index INDEX_" +columName+ " (" +columName+ ");" ; break ; case Newtonsoft.Json.Linq.JTokenType.Integer: mysqlType = "int(11) NULL" ; index = "ALTER TABLE " + tableName + " add index INDEX_" +columName+ " (" +columName+ ");" ; break ; case Newtonsoft.Json.Linq.JTokenType.String: mysqlType = "nvarchar(1000) " ; index = "ALTER TABLE " + tableName + " add index INDEX_" +columName+ " (" +columName+ ");" ; break ; case Newtonsoft.Json.Linq.JTokenType.Uri: mysqlType = "nvarchar(1000) " ; index = "ALTER TABLE " + tableName + " add index INDEX_" +columName+ " (" +columName+ ");" ; break ; default : mysqlType = "nvarchar(2000) " ; break ; } entity.Database.ExecuteSqlCommand( "alter table " + tableName + " add column " +columName+ " " + mysqlType + ";" + index); } } } } public class MysqlTableSchema { public string tables_in_test { get ; set ; } } public class MysqlColumSchema { public string field { get ; set ; } public string type { get ; set ; } public string key { get ; set ; } public string Null { get ; set ; } public string Extra { get ; set ; } } } |
分类:
MYSQL
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
2015-12-11 Setting up a Passive FTP Server in Windows Azure VM(ReplyCode: 227, Entering Passive Mode )
2015-12-11 Opening Default document on IIS (HTML With WebAPI)
2012-12-11 WCF客户端引用带有 int bool 类型的方法时,会自动加上一个Specified参数的 解决方法 Web Reference for a WCF Service has Extra “IdSpecified” Parameter -摘自网络
2012-12-11 WebService 设置成自定义实体 Web References 文件夹下 Reference.map下面的InvoiceDetailsViewModel.datasource文件里的 GenericObjectDataSource