VB.NET – Database Connection
Disclaimer: This work has been submitted by a student. This is not an example of the work written by professional academic writers. Here you can order a professional work. (Find a price that suits your requirements)
* Save 10% on First Order, discount promo code "096K2"
What we will learn today:
Creating a new database
Database connection using oledb
Retrieving data from database
Manipulating data in database from VB.NET application
Creating New Database and Table
Open your SQL Server Management Studio, and connect to your database engine {draw:frame}
After connecting, the screen will show you something like this {draw:frame}
We will create a new database, expand the ‘Databases’ folder, then right click on it, the choose ‘new database’ {draw:frame}
Type a new name for your database ‘forHCI’, and click ok (we will leave everything else by default)
Your new database should be created and shown in the object explorer.
{draw:frame}
Expand it and right click on the ‘tables’ folder and choose ’new table’. Type in three new column named ‘id_barang’, ‘nama_barang’, and ‘harga’, just like the screenshot below. {draw:frame}
Save your new table by clicking the save icon, name it ‘barang’. {draw:frame}
We want to fill the table for initial data to work with in the VB.NET application. Right click on the table ‘barang’ and choose ‘edit top 200 rows’, and type in some data. {draw:frame}
Alternatively you can insert data into the table from the query. You can open a new query window by clicking ‘new query’ button.
The Essay on Database Design Process Model Data Application
Abstract Database Systems has a practical, hands-on approach that makes it uniquely suited to providing a strong foundation in good database design practice. Database design is more art than science. While it's true that a properly designed database should follow the normal forms and the relational model, you still have to come up with a design that reflects the business you are trying to model. ...
{draw:frame}
Then type in insert query in the query window
{draw:frame}
The tables are ready for our practice. Database Connection
We will use the previous project (last week, MDI form) or alternatively you can create a new project.
If you use the previous project, create a new menu item and a new form and name it ‘database’ then add some sub menu item ‘Retrieve’ and ‘Insert Update Delete’
{draw:frame}
Create two new forms ‘frmretrieve’ and ‘frminsupdel’ and link it to the new menu item
Add a new control called ‘OleDbConnection’ from the toolbox and click anywhere on form1 or mainform
{draw:frame}
You should see something like this in your form1 or mainform {draw:frame}
Next, we want to set the connection to the database we just created. Click on the oledbconnection1 in the above screenshot, and look for ‘ConnectionString’ property in the properties, then click on the dropdown arrow and choose ‘new connection’ {draw:frame}
You should see a new form like below. {draw:frame}
Type in or choose the server name you use, choose ‘use windows authentication’, then select the database name we just created ‘forHCI’. Click ‘test connection’, if you do it right, it will tell you that the connection succeeded. Click ‘Ok’ to proceed.
Database connection is ready for use. Retrieving Data from Database
Open the ‘frmretrieve’
Design the form like the screenshot below
{draw:frame}
Name the textbox : txtId, txtNama, txtHarga Name the buttons : btnPrev, btnNext
We want to query our database and fetch the data from table ‘barang’ to the datatable. Double click on the form to create form load event handler
The Term Paper on Database And Database Management System
Database A database is an organized collection of data. The data are typically organized to model relevant aspects of reality in a way that supports processes requiring this information. For example, modeling the availability of rooms in hotels in a way that supports finding a hotel with vacancies. DBMS stands for “Database Management System.” In short, a DBMS is a database program. ...
Type in these codes
{draw:frame}
String sql is used to contain our query
Datatable is like a virtual table in your VB.NET, can be filled with any data, including data from a table from database
Dataadapter is like a bridge between the connection object (in this case, the oledbconnection1 we put in the form1 before) and the datatable (in this case, dt)
da.fill(dt), is used to fill the data queried from the database to the datatable
da.dispose() is like freeing a connection
Next, we want to display the data from datatable to the textboxes. Type in these codes just below the previous codes {draw:frame}
We can access the data in the datatable by the syntax:
datatable_name.Rows(row_index)(column_index)
in the example above, dt.rows(0)(1) means we point to the data stored in the datatable ‘dt’, in the first row, and second column.
Save your project and run it.
Next we want to put the data we queried to a datagridview. Type in these codes inside the form load sub procedure
{draw:frame}
Save your project and run it.
For exercise, try to make the next and previous button works. When user can navigate through the data in the datatable by clicking the next (>) and previous button (