How to Save Your Application Data in WPF Apps Using a Database
WPF, which stands for Windows Presentation Foundation, is a framework used for building client applications that run on Windows operating systems, was introduced as a part of .NET Framework 3.0.
The key aspect of WPF is that it uses C# as its backend language and XAML as its frontend language. It replaces Windows Forms, offering more security features and scalable UI elements for the wide range of applications it is used for.
Most of the applications built with WPF deal with data manipulation like saving or retrieving data from a database. This time around, we build our WPF application using Visual Studio and Oracle database. Our goal here is to properly connect to the database and after that, to save our data.
ODAC Installation

The first operation that needs to be performed is the connection to the Oracle Database and for that, the Oracle Data Access Components (ODAC) with Oracle Developer Tools for Visual Studio need to be downloaded from Oracle.com, database/technologies/section. This is a free extension that makes it easy to connect to the Oracle Database, browse and modify schema objects and data, edit and debug PL/SQL, and more. After downloading the corresponding release, the setup file must be run. The installation process consists of several steps:
- Product Languages: proper language must be selected
- Oracle User Selection: a windows built-in account can be used or a new user can be created
- Installation Locations: default or custom location can be used for storing software and configuration-related files
- Product Component: Oracle Data Provider for .NET and Oracle Developer Tools for Visual Studio must be chosen
- ODP.NET Configuration
- DB Connection Configuration: port 1521 must be specified for the port number
- Install Product
Adding Db Connection in VS

After successfully installing Oracle Client, we have to navigate to Server Explorer
in VS to add a new connection. This addition can be done by right-clicking on Data
Connections and selecting Add Connection. From the opened configuration panel, the
data source must be changed to Oracle Database and Data Provider set to ODP.NET,
Managed Driver. The username and password must be specified for the user schema
and EZ connect should be selected for connection type. According to Oracle installation,
connection details should be provided as follows:
- Database host name: localhost
- Port number: 1521
- Database service name: orcl
The connection can be tested, using the corresponding button, to assure that the
connection succeeds. After that, all schema objects can be browsed from VS.
Adding Reference to Oracle

After database connection and creating the application, a reference to Oracle must be added. This process can be done in the project by right-clicking on References and
selecting Add Reference.
From the reference manager, in the Assemblies-Extensions section, the
Oracle.ManagedDataAccess options must be chosen and after that, the OK button is pressed.
Adding Data Source

At this point, a new data source should be added from the Data Source window in VS, by clicking on the New Data Source button. The process is simple and straightforward. Here it must be taken into account that the data connection used for the applications to connect to the database should be provided. After that, the connection string variable must be named.
Modifying App.config file

A configurationString must be added to the configuration element together with an ‘add’ element to add a connection string with the specific properties: name, connectionString and providerName.
An instance of the connection object must be created in MainWindow.cs. A specific function can be defined for instantiating the Oracle connection object, where the connection string variable has to be created. The variable can be called from the App.config file. The next step is to create an Oracle connection object and pass the connection string to the constructor. Before InitializeComponent, we have to instantiate the connection object.
Inserting Information

Based on the data we have to add, from the toolbox section we have to choose the corresponding items. A button should also be added from the toolbox for the operation, in our case adding data to the database. An event listener on the click has to be created for the button from its properties window. On the event listener, the specific SQL statement for adding data to the database must be specified.
Conclusion

This article describes the steps required for saving data to a database in a WPF
application, from database connection to SQL statement. Let us know if you find this
helpful and if you have any additional questions.
