.net core3.1连接GBase数据库的步骤
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了.net core3.1连接GBase数据库的步骤,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3934字,纯文字阅读大概需要6分钟。
内容图文
![.net core3.1连接GBase数据库的步骤](/upload/InfoBanner/zyjiaocheng/525/334274263c35437bbf8bdd64c2721029.jpg)
4、添加数据库的helper类
public class GbaseBaseHelper { private GBaseConnection conn = null; private GBaseCommand cmd = null; private GBaseDataReader sdr = null; public GbaseBaseHelper() { string strConnectionString = ConfigHelper.GetSectionValue("ConnectionStrings:DefaultConnectionGBase"); conn = new GBaseConnection(strConnectionString); } private GBaseConnection GetConn() { try { if (conn.State == ConnectionState.Closed) { conn.Open(); } } catch (Exception ex) { throw ex; } return conn; } //关闭数据库连接 private void OutConn() { if (conn.State == ConnectionState.Open) { conn.Close(); } } /// 执行不带参数的增删改SQL语句或存储过程 public int ExecuteNonQuery(string cmdText, CommandType ct) { int res; try { cmd = new GBaseCommand(cmdText, GetConn()); cmd.CommandType = ct; res = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { OutConn(); } return res; } /// 执行带参数的增删改SQL语句或存储过程 public int ExecuteNonQuery(string cmdText, GBaseParameter[] paras, CommandType ct) { int res; try { cmd = new GBaseCommand(cmdText, GetConn()); cmd.CommandType = ct; cmd.Parameters.AddRange(paras); res = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { OutConn(); } return res; } /// 执行不带参数的查询SQL语句或存储过程 public DataTable ExecuteQuery(string cmdText, CommandType ct) { DataTable dt = new DataTable(); using (cmd = new GBaseCommand(cmdText, GetConn())) { cmd.CommandType = ct; cmd.CommandTimeout = 10 * 60;//10分钟 using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { dt.Load(sdr); } return dt; } } /// 执行带参数的查询SQL语句或存储过程 public DataTable ExecuteQuery(string cmdText, List<DbParameter> paras, CommandType ct) { DataTable dt = new DataTable(); using (cmd = new GBaseCommand(cmdText, GetConn())) { cmd.CommandType = ct; cmd.CommandTimeout = 10 * 60;//10分钟 if (paras != null) { cmd.Parameters.AddRange(paras.ToArray()); } using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { dt.Load(sdr); } return dt; } } public int ExecuteNonQueryByTransaction(List<string> lstSql) { int intResult = 0; using (GBaseConnection conn = GetConn()) { GBaseTransaction tran = null; try { using (tran = conn.BeginTransaction()) { if (lstSql != null && lstSql.Count > 0) { foreach (string sql in lstSql) { GBaseCommand cmd = new GBaseCommand(sql, conn); intResult += cmd.ExecuteNonQuery(); } } tran.Commit(); } } catch (GBaseException ex) { if (tran != null) tran.Rollback(); throw ex; } } return intResult; } }
备注:用到一个configHelper类
public static class ConfigHelper { private static IConfiguration _configuration; static ConfigHelper() { //在当前目录或者根目录中寻找appsettings.json文件 var fileName = "appsettings.json"; var directory = AppContext.BaseDirectory; directory = directory.Replace("\\", "/"); var filePath = $"{directory}/{fileName}"; if (!File.Exists(filePath)) { var length = directory.IndexOf("/bin"); filePath = $"{directory.Substring(0, length)}/{fileName}"; } var builder = new ConfigurationBuilder().AddJsonFile(filePath, false, true); _configuration = builder.Build(); }
public static string GetSectionValue(string key) { return _configuration.GetSection(key).Value; } }
5、添加引用“System.Text.Encoding.CodePages”
添加完后在入口处添加
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
6、添加引用“System.Diagnostics.PerformanceCounter”
7、测试效果
DataTable result = new GbaseBaseHelper().ExecuteQuery("select TABLE_NAME, TABLE_COMMENT from information_schema.tables where TABLE_SCHEMA = ‘" + tableSchema + "‘", CommandType.Text); return result;
.net core3.1连接GBase数据库的步骤
标签:exist 入口 ons rollback sql VID substr 当前目录 int
本文系统来源:https://www.cnblogs.com/wjx-blog/p/13175981.html
内容总结
以上是互联网集市为您收集整理的.net core3.1连接GBase数据库的步骤全部内容,希望文章能够帮你解决.net core3.1连接GBase数据库的步骤所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。