This tutorial describes how to connect to SQL Server.
In this walkthrough:
In order to connect to SQL Server you need the server itself running, dotConnect for SQL Server installed and IDE running. You should know server name or IP address, account login and password. Usually this information is enough.
Note that if you do not use design-time (specifically, if you do not place the SqlConnection component from the toolbox on a form designer), you have to embed licensing information manually. This is described in the Licensing topic.
To establish a connection to a server, you have to provide some connection parameters to dotConnect for SQL Server. This information is used by the SqlConnection component to find the server and login with credentials of your account. The parameters are represented as connection string. You can compose the connection string manually or have dotConnect for SQL Server construct it for you.
Design time creation
The following assumes that you have IDE running, and you are currently focused on a form designer.
Run time creation
Same operations performed in runtime look as follows (note that you have to add references to Devart.Data.SqlServer.dll and Devart.Data.dll assemblies):
You can do this all in single assignment. It actually does not matter whether connection string is assigned directly or composed with particular properties. After you assign a value to ConnectionString property all other properties are populated with parsed values. So you can choose what is more convenient for you.
Using connection string builder
If you decide to setup a connection by assigning values to several properties, consider using the SqlConnectionStringBuilder class. It has all of the possible connection settings exposed as properties, thus allowing you to customize the connection at full extent. The following example demonstrates how to compose a more complex connection string:
Note that in this example we used SqlConnection constructor that accepts connection string as argument.
For the information on arguments allowed in the connection string, refer to the description of the SqlConnection.ConnectionString property.
Opening a connection is as simple as that:
Of course, SqlConnection1 must have a valid connection string assigned earlier. When you call Open, dotConnect for SQL Server tries to find the host and connect to the server. If any problem occurs it raises an exception with brief explanation on what is wrong. If no problem is encountered dotConnect for SQL Server tries to establish the connection during ConnectionTimeout interval. Finally, when connection is established, the Open method returns and State property is changed to Open.
In design time you can connect to a server in few steps:
Or you can simply change the State property to Open in the Properties window to establish a connection using the current connection string.
To close a connection call its Close method, or set its State property to Closed.
The following example summarizes aforementioned information and shows how to create, setup, open, use and then close the connection.
The sample code connects to the server, shows its version and then closes the connection. This actually is rare usage, because in real applications connections are used by other objects like SqlCommand, SqlDataTable and others. For more information on this, please see the corresponding tutorials or the reference information.
You can modify connection by changing properties of SqlConnection object. Keep in mind that while some of the properties can be altered freely, most of them close connection when new value is assigned. For example, if you change Database property, connection remains open, but if you change DataSource property, it gets closed immediately, and you have to reopen it manually.
dotConnect for SQL Server has wide set of features you can take advantage of. The following list enumerates two of them so you can explore the advanced techniques to achieve better performance, balance network load or enable additional capabilities.