30.1 Creating Connection to a Database using ADO.NET in Visual Basic 2012
In Visual Basic 2012, we need to create a connection to a database before we can access its data. Before we begin, let’s create a new database. Since we are using SQL Server 2012 as the database engine, we shall use Microsoft Studio Management Express to create a database with the mdf extension. We shall name this database file test.mdf.
To initialize the variable to a new SqlConnection object, we use the following syntax:
Private MyCn As New SqlConnection
Having created the instance of the SqlConnecton object, the next step is to establish a connection to the data source using the SQL ConnectionString property. The syntax is:
MyCn.ConnectionString = "Data Source=TOSHIBA-PC\SQL2012; AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\DATA\Test.mdf; " & _ "User Instance=True;Integrated Security=SSPI"
* You need to change the reference to the SQL server (TOSHIBA-PC\SQL2012) as well as the path to database file Test.mdf .
After establishing the connection to the database, you can open the database using the following syntax:
30.2 Populating Data in ADO.NET
The next step is to create an instance of the SqlDataAdpater in our visual Basic 2012 code to populate the DataTable with data from the data source. Besides, you also need to create an instance of the DataTable. Other than that, you should also create an instance of the SqlCommandBuilder which is used to manipulate data such as updating and deleting data in the Datatable and send the changes back to the data source. The statements are:
Private MyDatAdp As New SqlDataAdapter Private MyCmdBld As New SqlCommandBuilder Private MyDataTbl As New DataTable
Besides that, we need to declare a variable to keep track of the user’s current row within the data table. The statement is
Private MyRowPosition As Integer = 0
Having created the above of objects, you need to include the following statements in the Sub Form_Load event to start filling the DataTable with data from the data source. The statements are as follows:
MyDatAdp = New SqlDataAdapter("Select* from Contacts", MyCn) MyCmdBld = New SqlCommandBuilder(MyDatAdp) MyDatAdp.Fill(MyDataTbl)
After filling up the DataTable , we need to write code to access the data. To access data in the DataTable means that we need to access the rows in the table. We can achieve this by using the DataRow object. For example, we can write the following to access the first row of the table and present the data via two text boxes with the name txtName and txtState respectively:
Dim MyDataRow As DataRow = MyDataTbl.Rows(0) Dim strName As String Dim strState As String strName = MyDataRow("ContactName") strState = MyDataRow("State") txtName.Text = strName.ToString txtState.Text = strState.ToStringMe.showRecords()
* The two fields being referenced here are ContactName and State. Note Index 0 means first row.
is a sub procedure created to show data in the textboxes? The code is as follows:
Private Sub showRecords() If MyDataTbl.Rows.Count = 0 Then txtName.Text = "" txtState.Text = "" Exit Sub End If txtName.Text = MyDataTbl.Rows(MyRowPosition)("ContactName").ToString txtState.Text = MyDataTbl.Rows(MyRowPosition)("State").ToString End Sub
Take a look at the output interface:
We will discuss how to manipulate data in the next visual basic 2012 lesson.