news 2026/7/7 5:58:57

C#通过sqlsugar插入数据到postgresql

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C#通过sqlsugar插入数据到postgresql

1.测试通过sql语句保存json数据,使用jsonb字段

public async Task<string> SavePostgreSQLTest(int count = 0,string files="") { string SqlStr = ""; string jsonParamStr = JsonConvert.SerializeObject(new { count = count, file = $"{files}", }); await Task.Run(() => { SqlStr = "INSERT INTO \"MyObj\".mytable (id, name, age,jsonb_result ,json_result) VALUES (@id, @Name, @Age,@JsonbResult::jsonb,@JsonResult)"; var parameters = new List<SugarParameter> { new SugarParameter("@id", 5), new SugarParameter("@Name", "周七"), new SugarParameter("@Age", 29), new SugarParameter("@JsonbResult", jsonParamStr), new SugarParameter("@JsonResult", jsonParamStr) }; try { SqlSugarHelperV1.ExcuteSqlString(SqlStr, parameters); } catch (Exception ex) { Log.Logger.Error($"LinkAsiaDB InitTireGlueingPLYDB 异常 {ex.Message} {ex.StackTrace}"); } }); return "jsonStr"; }

2.sqlsugarhelper代码如下

using SqlSugar; namespace aaa.Helper.DB { public static class SqlSugarHelperV1 { private static SqlSugarScope _db; static SqlSugarHelperV1() { string mysqlConnectionStr = AppSettingsHelper.Configuration["DB:PostgreSQLConnStr"] ?? ""; // 初始化数据库连接 _db = new SqlSugarScope(new ConnectionConfig() { ConnectionString = mysqlConnectionStr,// "server=localhost;port=3306;database=testdb;user=root;password=123456;", DbType = SqlSugar.DbType.PostgreSQL, IsAutoCloseConnection = true, // 自动关闭连接 // InitKeyType = InitKeyType.Attribute, // 从特性读取主键和自增列信息 //ConfigureExternalServices = new ConfigureExternalServices() //{ // EntityService = (c, p) => // { // // 设置列的默认值 // if (p.IsPrimarykey == false && p.PropertyName == "Id") // { // p.IsIgnore = true; // 忽略非主键的Id列 // } // } //} }); //// 设置AOP事件 //_db.Aop.OnLogExecuting = (sql, pars) => //{ // Console.WriteLine($"SQL: {sql}"); // Console.WriteLine($"Parameters: {string.Join(", ", pars.Select(p => $"{p.ParameterName}:{p.Value}"))}"); //}; _db.Aop.OnError = (exp) => { Console.WriteLine($"PostgreSQL Error: {exp.Sql}"); Console.WriteLine($"Error Message: {exp.Message}"); }; } // 获取数据库实例 public static SqlSugarScope Db => _db; // 查询多个对象(使用 SQL) public static List<T> GetList<T>(string sql, object parameters = null) where T : class, new() { return _db.Ado.SqlQuery<T>(sql, parameters); } public static void ExcuteSqlString(string sql, object parameters = null) { _db.Ado.ExecuteCommand(sql, parameters); } } }

postgresql的数据库表字段

下面是查询jsonb字段中的json中key的value值

select *,jsonb_result ->'name' as name1 from "MyObj".mytable;
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/2 3:55:41

导师推荐!自考必备8款AI论文软件深度测评

导师推荐&#xff01;自考必备8款AI论文软件深度测评 2026年自考论文写作工具测评&#xff1a;如何选到真正好用的AI助手 随着人工智能技术的不断进步&#xff0c;越来越多的自考学生开始依赖AI论文软件来提升写作效率和论文质量。然而&#xff0c;面对市场上琳琅满目的工具&am…

作者头像 李华
网站建设 2026/6/26 12:28:25

非线性时间序列复杂性与相似性分析【附代码】

✅ 博主简介&#xff1a;擅长数据搜集与处理、建模仿真、程序设计、仿真代码、论文写作与指导&#xff0c;毕业论文、期刊论文经验交流。 ✅成品或者定制&#xff0c;扫描文章底部微信二维码。 (1)基于累积剩余熵的时间序列复杂性量化技术 时间序列的复杂性反映了系统内在动力…

作者头像 李华
网站建设 2026/6/25 19:43:46

免费白嫖GPU!大模型微调训练全流程实战指南

本文是一篇面向小白的大模型微调实战教程&#xff0c;全程免费使用魔搭社区GPU算力。通过LLaMA Factory框架&#xff0c;从环境搭建、数据准备到模型微调&#xff0c;详细展示了如何利用少量特定数据对预训练模型进行微调&#xff0c;学习特定任务知识。整个过程仅需普通算力&a…

作者头像 李华
网站建设 2026/6/26 12:24:50

LLM-RL训练框架全攻略:四大主流框架横向评测与选型指南

本文深度解析LLM-RL训练面临的挑战与架构演变&#xff0c;详细对比TRL、OpenRLHF、verl和LLaMA Factory四大主流开源框架&#xff0c;涵盖架构设计、关键特性及适用场景&#xff0c;并介绍RAGEN、DeepSpeed等垂直解决方案。文章从性能、易用性和硬件需求三个维度提供横向评测与…

作者头像 李华