Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (31)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (28)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
Other Resources:
How can we connect to Microsoft Access , Foxpro , Oracle etc
How can we connect to Microsoft Access , Foxpro , Oracle etc ?
✍: Guest
Microsoft provides System.Data.OleDb namespace to communicate with databases like
scess , 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 Try objOLEDBCommand = New OleDbCommand(“Select FirstName from Employees”) objOLEDBCon.Open() objOLEDBCommand.Connection = objOLEDBCon objOLEDBReader = objOLEDBCommand.ExecuteReader() Do While objOLEDBReader.Read() lstNorthwinds.Items.Add(objOLEDBReader.GetString(0)) Loop Catch ex As Exception Throw ex Finally objOLEDBCon.Close() End Try End Sub
The main heart is the “Loaddata()” method which actually loads the data in listbox.
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.
2007-10-24, 6245👍, 0💬
Popular Posts:
How To View All Columns in an Existing Table? - Oracle DBA FAQ - Managing Oracle Database Tables If ...
If client side validation is enabled in your Web page, does that mean server side code is not run? W...
If XML does not have closing tag will it work? No, every tag in XML which is opened should have a cl...
How can we suppress a finalize method? GC.SuppressFinalize ()
Describe different elements in Static Chart diagrams ? Package: - It logically groups element of a U...