How can we connect to Microsoft Access, FoxPro, and Oracle etc?

 Posted by ArticlesMaint on 9/16/2009 | Category: ADO.NET Interview questions | Views: 4558

Answer: -
Microsoft provides System.Data.OleDb namespace to communicate with databases like success , Oracle etc. In short, any OLE DB-Compliant database can be connected using System.Data.OldDb namespace.

Note :- Small sample of OLEDB is provided in “WindowsAppOleDb” which uses “Nwind.mdb” in bin directory to display data in Listbox.

Private Sub loadData() 

Dim strPath As String
strPath = AppDomain.CurrentDomain.BaseDirectory
Dim objOLEDBCon As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source =” & strPath & “Nwind.mdb”)
Dim objOLEDBCommand As OleDbCommand
Dim objOLEDBReader As OleDbDataReader

objOLEDBCommand = New OleDbCommand(“Select FirstName from Employees”)
objOLEDBCommand.Connection = objOLEDBCon
objOLEDBReader = objOLEDBCommand.ExecuteReader()

Do While objOLEDBReader.Read()
Catch ex As Exception
Throw ex
End Try

End Sub

The main heart is the “Load data ()” method which actually loads the data in list box.

Note: - This source code has the connectionstring hard coded in the program itself which is not a good programming practice. For windows application the best place to store connectionstring is “App.config”. Also note that “AppDomain.CurrentDomain.BaseDirectory” function gives the current path of the running exe which is “BIN” and the MDB file is in that directory. Also note that the final block which executes irrespective that there is error or not. Thus ensuring that all the connection to the datastore is freed. Its best practice to put all clean up statements in finally block thus ensuring that the resources are deallocated properly.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response