码迷,mamicode.com
首页 > 数据库 > 详细

.net core3.1连接GBase数据库的步骤

时间:2020-06-22 13:06:28      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:exist   入口   ons   rollback   sql   VID   substr   当前目录   int   

1、添加Gbase官方提供的dll文件  “GBase.Data.dll”的引用

添加完如下图:

技术图片

2、添加引用“Microsoft.Extensions.Configuration.Json”

3、添加连接字符串

{
  "ConnectionStrings": {"DefaultConnectionGBase": "Server=21.19.12.8;Port=5258;Database=myDb;User Id=test;Password=test123456;SslMode=None;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!