Beispiel #1
0
        public async Task BatchExcuteAsync(params sql.SqlCommand[] commands)  //IContextInfo contextInfo,
        {
            if (commands?.Length < 1)
            {
                return;
            }

            using (var connection = new sql.SqlConnection(_connectionString))
            {
                //connection.StateChange += new System.Data.StateChangeEventHandler((sender, e) =>
                //{
                //    if (e.CurrentState == System.Data.ConnectionState.Open)
                //        setContextInfo(connection, contextInfo);

                //});

                await connection.TryOpenAsync();

                using (var tran = connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
                {
                    try
                    {
                        foreach (var command in commands)
                        {
                            command.Connection  = connection;
                            command.Transaction = tran;
                            await command.ExecuteNonQueryAsync();
                        }

                        tran.Commit();
                    }
                    catch (Exception e)
                    {
                        tran.Rollback();
                        throw e;
                    }
                    finally
                    {
                        //clearContextInfo(connection, contextInfo);
                        connection.TryClose();
                    }
                }
            }
        }
Beispiel #2
0
        protected async Task <System.Data.DataSet> ExecuteAsync(sql.SqlCommand command)   //, IContextInfo contextInfo
        {
            var dataSet = new System.Data.DataSet();

            using (var connection = new sql.SqlConnection(_connectionString))
            {
                //connection.StateChange += new System.Data.StateChangeEventHandler((sender, e) =>
                //{
                //    if (e.CurrentState == System.Data.ConnectionState.Open)
                //        setContextInfo(connection, contextInfo);
                //});

                command.Connection = connection;
                await connection.TryOpenAsync();

                using (var reader = await command.ExecuteReaderAsync())
                {
                    do
                    {
                        if (reader.HasRows && reader.FieldCount > 0)
                        {
                            using (var table = dataSet.Tables.Add())
                            {
                                table.Load(reader);
                            }
                        }
                        else
                        {
                            reader.Close();
                        }
                    } while (!reader.IsClosed);
                }

                //clearContextInfo(connection, contextInfo);
                connection.TryClose();
            }

            return(dataSet);
        }