Exemplo n.º 1
0
 /// <summary>
 /// Dispose the connection
 /// </summary>
 public virtual void Dispose()
 {
     OpenedDataReaders.Clear();
     _attachedObjects.Clear();
     Trans?.Dispose();
     SqlConnection?.Dispose();
     Trans         = null;
     SqlConnection = null;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Validate Connection is Open or broken then reopen it
        /// </summary>
        protected void ValidateConnection()
        {
            try
            {
                if (SqlConnection == null)
                {
                    SqlConnection = new TransactionSqlConnection(DataBaseTypes, ConnectionString);
                }

                if (SqlConnection.State == ConnectionState.Broken || SqlConnection.State == ConnectionState.Closed)
                {
                    SqlConnection.Open();
                }
            }
            catch (Exception e)
            {
                throw new EntityException(e.Message);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// commit the transaction
 /// </summary>
 public void SaveChanges()
 {
     try
     {
         Trans?.Commit();
     }
     catch (Exception ex)
     {
         Rollback();
         throw new EntityException(ex.Message);
     }
     finally
     {
         OpenedDataReaders.Clear();
         Trans?.Dispose();
         SqlConnection?.Dispose();
         Trans         = null;
         SqlConnection = null;
     }
 }
Exemplo n.º 4
0
        internal static ISqlCommand ProcessSql(this IRepository repository, TransactionSqlConnection connection, IDbTransaction tran, string sql)
        {
            var             i       = 1;
            var             dicCols = new SafeValueType <string, Tuple <object, SqlDbType> >();
            MatchCollection matches = null;

            while ((matches = stringExp.Matches(sql)).Count > 0)
            {
                var    exp = matches[0];
                var    col = "@CO" + i + "L";
                object str = exp.Value.TrimEnd(']').Substring(@"String\[".Length - 1);
                sql = sql.Remove(exp.Index, exp.Value.Length);
                sql = sql.Insert(exp.Index, col);
                var v = str.ConvertValue <string>();
                if (string.Equals(v, "null", StringComparison.OrdinalIgnoreCase))
                {
                    v = null;
                }
                dicCols.TryAdd(col, new Tuple <object, SqlDbType>(v, SqlDbType.NVarChar));
                i++;
            }

            while ((matches = dateExp.Matches(sql)).Count > 0)
            {
                var    exp = matches[0];
                var    col = "@CO" + i + "L";
                object str = exp.Value.TrimEnd(']').Substring(@"Date\[".Length - 1);
                sql = sql.Remove(exp.Index, exp.Value.Length);
                sql = sql.Insert(exp.Index, col);
                dicCols.TryAdd(col, new Tuple <object, SqlDbType>(str.ConvertValue <DateTime?>(), SqlDbType.DateTime));
                i++;
            }

            while ((matches = guidExp.Matches(sql)).Count > 0)
            {
                var    exp = matches[0];
                var    col = "@CO" + i + "L";
                object str = exp.Value.TrimEnd(']').Substring(@"Guid\[".Length - 1);
                sql = sql.Remove(exp.Index, exp.Value.Length);
                sql = sql.Insert(exp.Index, col);
                dicCols.TryAdd(col, new Tuple <object, SqlDbType>(str.ConvertValue <Guid?>(), SqlDbType.UniqueIdentifier));
                i++;
            }

            sql = sql.CleanValidSqlName(repository.DataBaseTypes);
            DbCommand cmd = null;

            try
            {
                switch (repository.DataBaseTypes)
                {
                case DataBaseTypes.Mssql:
                    cmd = tran != null ?
                          "System.Data.SqlClient.SqlCommand".GetObjectType("System.Data.SqlClient").CreateInstance(new object[] { sql, connection.DBConnection, tran }) as DbCommand :
                          "System.Data.SqlClient.SqlCommand".GetObjectType("System.Data.SqlClient").CreateInstance(new object[] { sql, connection.DBConnection }) as DbCommand;
                    break;

                case DataBaseTypes.PostgreSql:
                    cmd = tran != null ?
                          "Npgsql.NpgsqlCommand".GetObjectType("Npgsql").CreateInstance(new object[] { sql, connection.DBConnection, tran }) as DbCommand :
                          "Npgsql.NpgsqlCommand".GetObjectType("Npgsql").CreateInstance(new object[] { sql, connection.DBConnection }) as DbCommand;
                    break;

                case DataBaseTypes.Sqllight:
                    cmd = tran != null ?
                          "System.Data.SQLite.SQLiteCommand".GetObjectType("System.Data.SQLite").CreateInstance(new object[] { sql, connection.DBConnection, tran }) as DbCommand :
                          "System.Data.SQLite.SQLiteCommand".GetObjectType("System.Data.SQLite").CreateInstance(new object[] { sql, connection.DBConnection }) as DbCommand;
                    break;
                }
            }
            catch (Exception e)
            {
                switch (repository.DataBaseTypes)
                {
                case DataBaseTypes.Mssql:
                    throw new EntityException($"Please make sure that nuget 'System.Data.SqlClient' is installed \n orginal exception: \n {e.Message}");

                case DataBaseTypes.PostgreSql:
                    throw new EntityException($"Please make sure that nuget 'Npgsql' is installed \n orginal exception: \n {e.Message}");

                case DataBaseTypes.Sqllight:
                    throw new EntityException($"Please make sure that nuget 'System.Data.SQLite' is installed \n orginal exception: \n {e.Message}");
                }
            }

            var dbCommandExtended = new DbCommandExtended(cmd, repository);

            foreach (var dic in dicCols)
            {
                dbCommandExtended.AddInnerParameter(dic.Key, dic.Value.Item1);
            }
            return(dbCommandExtended);
        }