Properties of OLEDBConnection and SQLConnection classes
Methods of OLEDBConnection and SQLConnection classes
.NET has brought so many changes to the way one writes programs. Once of the key changes has been ADO.NET. It is an improvement over ADO. The following are the some of the problems with ADOs.
Microsoft has always been a bit unfair on programmers when it comes to data access. Just when you felt you have mastered the current one, you have new one on your table. This has happened with DAO (Data Access Objects) and RDO (Remote Data Objects). And now when you feel you are conversant with ADO, you have ADO.NET to start again from scratch (at least in some areas).
Let us now see what is new in ADO.NET. I will just give you some idea about new features now, only to give a feel of what is to come. I will get into all the details in this article and in the remaining articles.
That means the connection time to database is kept to absolute minimum. This is an important development since connections to the database is a bottleneck. This process allows more number of clients to connect to database thus increasing scalability (the ability to scale application up/down) of the application. That also means though more number of clients are accessing the database, the number of connections at any given time will not significantly increase and thus enabling system provide more scalability.
ADO.NET supports Dataset, which is a mini database in memory. It supports constraints and relationship between tables in memory. This enables master-detail relationship between tables in memory.
Apart from the ADO.NET stores data in XML format internally and transmits data using XML. This enables any application – whether or not ADO.NET based – can receive the data sent by an ADO.NET component. This becomes an important feature while developing Web Services, which are based on protocol SOAP that also uses XML.
Right now only two managed providers are available.
SQL Server .NET Data Provider – this is used to access SQL Server. The classes related to this provider are provides through SYSTEM.DATA.SQLCLIENT namespace.
OLE DB .NET Data Provider – is to internally access OLE DB provider. We have to use OLE DB.NET Data Provider until we get a provider that directly accesses the database. This is used in cases where a true provider is not available. This is similar to the concept of OLE DB provider for ODBC drivers. This is provided through SYSTEM.DATA.OLEDB namespace.
Apart from these two providers, you may also need to include SYSTEM.DATA namespace to make some more classes of ADO.NET available to your program.
All the programs are written as console programs in VB.NET. I am using .NET Beta 2. If you like, you can make use of any text editor such as Notepad or you can even use Visual Studio.NET for the same.
The following is a simple program to access MS Access database – PHONES.MDB.
In the above example we are using OLE DB .NET Data Provider to access MS Access database.
The actual data access is done through OLE DB provider for MS Access.
The following are the important steps in the above example.
It uses SYSTEM.DATA.SQLCLIENT namespace, which contains SQL Server .NET Data Provider.
It used SQLConnection class is used instead of OLEDBConnection class.
ConnectionString is different as we now access SQL Server.
The following are some of the most commonly used properties and methods of these connection classes.
What is ADO.NET?
Since data access has become very important in all types of applications – windows and web, Microsoft has brought out a new data access technology – ADO.NET – to access data. This is successor to ADO, which was used to access data prior to .NET.
What’s wrong with ADOs?
Though ADO was used in windows and web applications for quite some time, it suffers from the following limitations.
Whether you have faced with any of the problems mentioned above or not, you have to more on to ADO.NET. If not anything else (assuming you don’t need anything more than what ADO provides) at least it will provide all that your ADO is providing.
Why should I move to ADO.NET?
The answer is very simple. Because .NET is the future for programmers working with Microsoft technologies. So, you have to move on to new technologies that are made available on .NET. ADO is gone and now ADO.NET. Don’t panic. It is not so bad. I mean ADO.NET does come with some new interesting and important features.
Scalability
This is very crucial for Web applications. ADO.NET provides a new component called DataSet. It doesn’t need constant connection to the database. It connects to database, retrieves data and disconnects from database. It allows the data stored in memory to be manipulated. It again connects to the database when changes made to data in memory are to be made to database.
Performance
ADO.NET increases performance. It uses XML to transmit the data whereas ADO used COM marshalling. COM marshalling spent a lot of time in converting data types of the database to data type defined in COM. This overhead removed from ADO.NET.
XML support is built-in
As XML is becoming new way of storing information, XML support in ADO.NET is much wanted. ADO.NET supports storing data in XML documents and retrieving information from XML documents.
Managed providers
A managed provider is a set of ADO.NET objects designed to access a database. Managed providers run in .NET and access database directly using native API of the database.
Connecting to database
Now, let us write our first program to access an MS Access database. However, remember you can
modify the program to access any database. The only area where you have to change is the
"connection string". If you want to use SQL managed provider to access SQL Server then you
have to change even classes. We will discuss more about it later.
' import required namespaces
Imports system
Imports System.data
Imports system.data.oledb
Module OLEDBConExample
Public Sub Main()
dim constr as string = "provider=Microsoft.jet.oledb.4.0;Data source=c:\srikanth\phones.mdb"
dim con as New OLEDBConnection(constr)
Try
con.Open()
console.WriteLine("Connection is established")
Catch ex As Exception
console.WriteLine(ex.Message)
Finally
' close connection if it is open
If con.State <> ConnectionState.Closed Then
console.WriteLine("Closing the connection")
con.Close()
End If
End Try
End Sub
End Module
Type the program and save it under a name. I am using the file name OLEDBCONEXAMPLE.VB.
You can use any other name. Then compile the program at command prompt using the following
command.
c:\srikanth>vbc /r:system.dll /r:system.data.dll /r:system.xml.dll oledbconexample.vb
The switch /r is used to refer to resources to be used by the compiler.
SYSTEM.DLL and SYSTEM.DATA.DLL are the assemblies(program units) that contain the classes
the program is using.
Running the above program will give the following output.
' import required namespaces
Imports system
Imports System.data
Imports system.data.oledb
dim constr as string = "provider=Microsoft.jet.oledb.4.0;Data source=c:\srikanth\phones.mdb"
dim con as New OLEDBConnection(constr)
c:\srikanth>OLEDBCONExample
Connection is established
Closing the connection
If the connection is not established for any reason then the program displays the error as follows.
C:\srikanth>OLEDBCONExample
Could not find file 'c:\srikanth\phones.md'.
The following example will show how to connect to SQL Server database using SQLConnection class.
' import required namespaces
Imports system
Imports system.data.SQLClient ' SQL managed provider
Module SQLConExample
Public Sub Main()
Dim con As New SQLConnection()
Try
con.ConnectionString = "server=localhost;uid=sa;pwd=;database=pubs"
con.Open()
console.WriteLine("Connection is established")
con.Close()
Catch ex As Exception
console.WriteLine(ex.Message)
End Try
End Sub
End Module
As you can notice the above program is almost similar to the previous one. The following are the differences.
Properties of OLEDBConnection and SQLConnection classes
The following are important properties of OLEDBConnection and SQLConnection classes.
Property | Meaning |
---|---|
ConnectionString | Gets or sets the string used to open a data store. |
ConnectionTimeout | Specifies the amount of time in seconds to wait before terminating attempt and display error. Read-only property |
Database | Name of the database to be used. Read-only property |
DataSource | Name of the database file to connect to. Read-only property |
Provider | Provider to be used to connect. Not available with SQLConnection class. Read-only property. |
State | Contains the state of the connection – Broken, Closed, Connecting, Executing, Fetching,Open. |
ServerVersion | Returns the version of the server to which client is connected. |
Method | Meaning |
---|---|
BeginTransaction() | Begins a database transaction and returns an object of OLEDBTransaction class |
Close() | Closes the connection to the database. |
Open() | Opens a database connection. |
CreateCommand() | Creates an object of OLEDBCommand class and returns it |
ChangeDatabase(string) | Allows the current database to be changed for the current connection. |
Note: Please consult .NET documentation for SQLConnection and OLEDBConnection to get comprehensive documentation on these classes.
That's all for now. In the next articles I will discuss more about how to retrieve data using ADO.NET.
P.Srikanth.
I request you to tell me how you found this article. Please click here to send your feedback. Your feedback is very valuable and enables me to serve you better.