30.1 Creating Connection to a Database using ADO.NET
In Visual Basic 2010, we need to create 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 2008 as the database engine, we will use Microsoft Studio Management Express to create a database with the mdf extension. We shall name this database file as test.mdf. After creating the database, build a table called Contacts and create two fields and name them ContactName and State respectively. Enter a few data in the table and click Save All to save the data. Now we are ready to connect to this new database.
MyCn.ConnectionString = “Data Source=lenovo-4903350b\mssmlbiz; AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Test.mdf; ” & _
“User Instance=True;Integrated Security=SSPI”
* You need to change the reference to the SQL server (lenovo-4903350b\mssmlbiz) as well as the path to database file Test.mdf .
After establishing connection to the database, you can open the database using the following syntax:
30.2 Populating Data in ADO.NET
Establishing connection to a database in Visual Basic 2010 using SqlConnection alone will not present anything tangible things to the user to manipulate the data until we add more relevant objects and write relevant codes to the project.
The next step is to create an instance of the SqlDataAdpater in our code so that we can 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
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)
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:
* The two fields being referenced here are ContactName and State. Note Index 0 means first row.
showRecords() is a sub procedure created to show data in the text boxes. The code is as follows:
Private Sub showRecords()
If MyDataTbl.Rows.Count = 0 Then
txtName.Text = “”
txtState.Text = “”
txtName.Text = MyDataTbl.Rows(MyRowPosition)(“ContactName”).ToString
txtState.Text = MyDataTbl.Rows(MyRowPosition)(“State”).ToString
The output interface:
We will discuss how to manipulate data in the next lesson.