コード例 #1
0
        public static IDbConnection GetDbConnection(IDataSourceDb databse)
        {
            IDbConnection connection = null;

            switch (databse.DBTYPE)
            {
            case DataBaseType.SqlServer:
                connection = new SqlConnection();
                break;

            case DataBaseType.MySql:
            case DataBaseType.Sqlite:
                connection = databse.GetNewConnection(false, true);
                break;

            case DataBaseType.Oracle:
                break;

            case DataBaseType.Oledb:
#if NETFRAMEWORK
                connection = new OleDbConnection();
                break;
#else
                throw new NotImplementedException($@".Net Standard haven't implemented this functionality yet.");
#endif

            case DataBaseType.Access95:
#if NETFRAMEWORK
                connection = new OleDbConnection();
                break;
#else
                throw new NotImplementedException($@".Net Standard haven't implemented this functionality yet.");
#endif

            case DataBaseType.Odbc:
#if NETFRAMEWORK
                connection = new OdbcConnection();
                break;
#else
                throw new NotImplementedException($@".Net Standard haven't implemented this functionality yet.");
#endif

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (connection != null)
            {
                connection.ConnectionString = databse.BuildConnectionString();
            }
            return(connection);
        }
コード例 #2
0
        /// <summary>
        /// Executes the non query on list.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="listPoco">The list poco.</param>
        /// <param name="buildSqlString">The build SQL string.</param>
        /// <returns>Numbers Of Rows Affected</returns>
        private static int ExecuteNonQueryOnListStandard <T>(IDataSourceDb database, string tableName, IEnumerable <T> listPoco, Action <StringBuilder, string, T, Expression <Func <T, object> > > buildSqlString, Expression <Func <T, object> > overrideKeys = null) where T : class
        {
            var records = 0;


            //     if (!EnableTransactionRollback && DBTYPE == DataBaseType.SqlServer) return BulkInsert(listPoco, tableName); TODO :: This triggers on any action type not insert only fix this

            using (var conn = database.GetNewConnection(false, true))
            {
                conn.Open();
                using (var trans = conn.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    var startTime = DateTime.Now;


                    foreach (var poco in listPoco)
                    {
                        try
                        {
                            var sqlBuilder = new StringBuilder();
                            buildSqlString(sqlBuilder, tableName, poco, overrideKeys);
                            var query = sqlBuilder.ToString();
                            using (var cmd = database.GetNewCommand(query, conn, trans))
                            {
                                var bucket = database.LogConnectionTime(conn, query);

                                var DbParameters = new ObjectToSqlHelper(database.DBTYPE).BuildDbParameterList(poco, database.GetNewParameter, database.XmlSerializer, database.JsonSerializer, database.CsvSerializer);
                                cmd.Parameters.AddRange(DbParameters.ToArray());
                                if (database.QueryBucketManager.IncludeReadableQuery)
                                {
                                    bucket.ReadableQuery = cmd.Parameters.ParamToSql(sqlBuilder.ToString());
                                }

                                records += cmd.ExecuteNonQuery();
                                bucket.ExecutedSuccesfully = true;
                            }
                        }
                        catch (Exception error)
                        {
                            if (database.EnableTransactionRollback)
                            {
                                trans.Rollback();
                                var endTime    = DateTime.Now;
                                var difference = endTime.Subtract(startTime);

                                return(records);
                            }
                            else
                            {
                                throw database.ErrorHandling(error);
                            }
                        }
                    }
                    if (records >= 0)
                    {
                        trans.Commit();
                        var endTime    = DateTime.Now;
                        var difference = endTime.Subtract(startTime);
                    }
                }
            }
            return(records);
        }