How to refresh the database connection in WPF application.

Santhosh Badam 20 Reputation points
2024-06-19T04:59:41.0466667+00:00

I have a Oracle19c database is connected to 4 machines having WPF applications with MVVM pattern and follows entity frame work. So from this following query SELECT MACHINE FROM v$session WHERE USERNAME = 'C##SAN' AND STATUS = 'ACTIVE';

and also using different parameters able to the other information.

Need to create a link by clicking the link the database connection for all the machines should refresh. How can I achieve . Please help me for this.

0 commentsNo commentsReport a concern

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,703 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,813 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 47,176 Reputation points Microsoft Vendor
    2024-06-19T08:31:20.0366667+00:00

    Hi,@Santhosh Badam. Welcome to Microsoft Q&A.

    Here is a complete example of how to implement database connection refresh functionality in a WPF application using the MVVM pattern and Entity Framework. You could try to refer to it.

    xaml:

    
     <StackPanel>
    
         <Button Content="Refresh Database" Command="{Binding RefreshCommand}" />
    
     </StackPanel>
    

    Codebehind:

    using Microsoft.EntityFrameworkCore;
    using System.ComponentModel;
    using System.Text;
    using System.Timers;
    using System.Windows;
    
    using System.Windows.Input;
    
    namespace WpfApp4
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = new MainViewModel();
            }
        }
     
       
        public class MainViewModel : INotifyPropertyChanged
        {
            public ICommand RefreshCommand { get; }
    
            public MainViewModel()
            {
                RefreshCommand = new RelayCommand(async () => await RefreshDatabaseConnectionsAsync());
            }
    
            private async Task RefreshDatabaseConnectionsAsync()
            {
                await Task.Run(() =>
                {
                    using (var context = new ApplicationDbContext())
                    {
                        context.Database.CloseConnection();
                        context.Database.OpenConnection();
                    }
                });
    
                OnDatabaseRefreshed();
            }
    
            private void OnDatabaseRefreshed()
            {
                // Notify other parts of your application if needed
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public class RelayCommand : ICommand
        {
            private readonly Action _execute;
            private readonly Func<bool> _canExecute;
    
            public event EventHandler CanExecuteChanged;
    
            public RelayCommand(Action execute, Func<bool> canExecute = null)
            {
                _execute = execute ?? throw new ArgumentNullException(nameof(execute));
                _canExecute = canExecute;
            }
    
            public bool CanExecute(object parameter)
            {
                return _canExecute == null || _canExecute();
            }
    
            public void Execute(object parameter)
            {
                _execute();
            }
    
            public void RaiseCanExecuteChanged()
            {
                CanExecuteChanged?.Invoke(this, EventArgs.Empty);
            }
        }
    
    
        public class ApplicationDbContext : DbContext
        {
            public ApplicationDbContext() : base()
            {
                // Initialize the DbContext as needed
            }
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseOracle("Your Oracle Connection String Here");
            }
        }
    }
    
    
    

    Best Regards,

    Hui


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful