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

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;

 

posted @ 2020-06-22 11:31  星星c#  阅读(1388)  评论(5编辑  收藏  举报