|
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using FreeSql;
- using CnasSynchronousCommon;
- using System.Data.Entity;
-
- namespace CnasSynchronusDAL
- {
- public static class KingbaseHelper
- {
- private static IFreeSql fsql;
- public static string strNameSpace = string.Empty;
- private static char strBiao = '.';
-
- public static (string, string) GetDatabaseAndNamespace(string strServerHost)
- {
- if (strServerHost.Contains(strBiao))
- {
- return (strServerHost.Split(strBiao)[0], strServerHost.Split(strBiao)[1]);
-
- }
- return ("", "");
- }
-
- public static void InitConnectionString(string strServerIP, string strServerPort, string strServerHost, string strServerUser, string strServerPwd)
- {
- var databaseAndNamespace = GetDatabaseAndNamespace(strServerHost);
- strNameSpace = databaseAndNamespace.Item2;
- string connectionString = $"Host={strServerIP};Port={strServerPort};Database={databaseAndNamespace.Item1};Username={strServerUser};Password={strServerPwd};";
-
-
-
- InitFreeSql(connectionString);
- }
-
- public static void InitConnectionString(string connectionString)
- {
- InitFreeSql(connectionString);
- }
-
- private static void InitFreeSql(string connectionString)
- {
- fsql = new FreeSqlBuilder()
- .UseConnectionString(DataType.KingbaseES, connectionString)
- .UseAutoSyncStructure(false) // 不自动同步结构
- .Build();
- }
-
- /// <summary>
- /// 执行SQL语句,返回影响的行数
- /// </summary>
- public static int ExecuteNonQuery(string sql, object parameters = null)
- {
- try
- {
- return fsql.Ado.ExecuteNonQuery(sql, parameters);
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- throw;
- }
- }
-
- /// <summary>
- /// 执行SQL语句,返回第一行第一列的值
- /// </summary>
- public static T ExecuteScalar<T>(string sql, object parameters = null)
- {
- try
- {
- //return fsql.Ado.ExecuteScalar<T>(sql, parameters);
-
- return default(T);
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- throw;
- }
- }
-
- /// <summary>
- /// 执行SQL查询,返回DataTable
- /// </summary>
- public static DataTable ExecuteDataTable(string sql, object parameters = null)
- {
- try
- {
- return fsql.Ado.ExecuteDataTable(sql, parameters);
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- return null;
- }
- }
-
- /// <summary>
- /// 执行SQL查询,返回DataSet
- /// </summary>
- public static DataSet ExecuteDataSet(string sql, object parameters = null)
- {
- try
- {
- return fsql.Ado.ExecuteDataSet(sql, parameters);
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- throw;
- }
- }
-
- /// <summary>
- /// 测试数据库连接
- /// </summary>
- public static bool TestConnectKingbase()
- {
- try
- {
- string connStr = "Host=localhost;Port=54321;Database=test;Username=system;Password=1;";
- using (var fsql = new FreeSqlBuilder()
- .UseConnectionString(DataType.KingbaseES, connStr)
- .Build())
- {
- return (int)fsql.Ado.ExecuteScalar("SELECT 1") == 1;
-
- }
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- return false;
- }
- }
- }
- }
|