Database Control dotConnectforSQLServer Tutorial: how to connect to SQLS

Source: Internet
Author: User
Tags how to connect to sql server
DotConnectforSQLServer is easy to operate and fast. This article describes how to connect to SQLServer and the issues that should be paid attention to during the period. To connect to SQLServer, you need to run the server, install dotConnectforSQLServer, and run IDE. You need to know the server name or IP address, Logon account and password. Generally

DotConnect for SQL Server is easy to operate and fast. This article describes how to connect to SQL Server and issues that should be paid attention to during the period. To connect to SQL Server, you must run the Server, install dotConnect for SQL Server, and run the IDE. You need to know the server name or IP address, Logon account and password. Generally

DotConnect for SQL Server is convenient and quick to operate. This article describesHowConnectionToSQL ServerAnd issues that should be paid attention to during the period.

Requirements
ToConnectionTo SQL Server, you need to run the Server, install dotConnect for SQL Server, and run IDE. You need to know the server name or IP address, Logon account and password. Generally, this information is enough.
Note,If you do not need to design,You have to manually embed the license information.

Basic Information
To createConnection, You need to provide someConnectionParameters are usedConnectionTo dotConnect for SQL Server. This information is mainly used by the SqlConnection control to find the server andUseLog on to the authentication information created by the account. TheConnectionString representation, of course, you can also manually write theseConnectionString or let dotConnect for SQL Server build.

CreateSqlConnection
Creation During Design:
The following procedure assumes that you are focusing on the table designer when the IDE is already running:
1. Open the toolbox and find the SqlConnection component for the SQL Server class in dotConnect.
2. Double-click the component and a new object will be displayed in the designer below the table. If you create SqlConnection for the first time in the application, name itSqlConnection1.
3.SqlConnection1Click on the object and click F4 follow object attributes.
4. the DNS name or IP address of the computer on which SQL Server is located will be provided in the data source attribute.
5.DatabaseAttribute specifies that you areConnectionOfDatabase.
6. Specify your login name in the user Id attribute.
7. Specify your password in the Password attribute.
8. Note that when you assign a value to an attribute, the ConnectionString attribute is automatically updated to reflect your settings.

Creation at runtime
Perform the same operation at runtime as follows:
[C #]

1

2

3

4

5

6

7

Using Devart. Data. SqlServer;

...

SqlConnection sqlConnection1 = new SqlConnection ();

SqlConnection1.DataSource = "127.0.0.1 ";

SqlConnection1.Database = "Test ";

SqlConnection1.UserId = "sa ";

SqlConnection1.Password = "mypassword ";

[Visual Basic]

1

2

3

4

5

6

7

Imports Devart. Data. SqlServer

...

Dim SqlConnection1 As SqlConnection = New SqlConnection ()

SqlConnection1.DataSource = "127.0.0.1"

SqlConnection1.Database = "Test"

SqlConnection1.UserId = "root"

SqlConnection1.Password = "mypassword"

You can do this in a single task.ConnectionWhether a string is directly allocated or combined with a special attribute does not affect the function. After you assign a value to the ConnectionString attribute, all other values will be filled with the parsed values. Therefore, you can choose more convenient for you.

[C #]

1

SqlConnection1.ConnectionString = "User Id = sa; Password = mypassword; DataSource = 127.0.0.1 ;";

[Visual Basic]

1

SqlConnection1.ConnectionString = "User Id = root; Password = mypassword; DataSource = 127.0.0.1 ;"

UseConnectionString Generator
If you decide to createConnection, You can considerUseSqlConnectionStringBuilder class. It contains allConnectionTherefore, you can customize links in all ranges. The following example showsHowConstitute a more complexConnectionString:

[C #]

1

2

3

4

5

6

7

8

SqlConnectionStringBuilder myCSB = new SqlConnectionStringBuilder ();

MyCSB. DataSource = "127.0.0.1 ";

MyCSB. UserId = "sa ";

MyCSB. Password = "mypassword ";

MyCSB. Database = "Test ";

MyCSB. MaxPoolSize = 150;

MyCSB. ConnectionTimeout = 30;

SqlConnection myConnection = new SqlConnection (myCSB. ConnectionString );

[Visual Basic]

1

2

3

4

5

6

7

8

Dim myCSB As SqlConnectionStringBuilder = New SqlConnectionStringBuilder

MyCSB. DataSource = "127.0.0.1"

MyCSB. UserId = "sa"

MyCSB. Password = "mypassword"

MyCSB. Database = "Test"

MyCSB. MaxPoolSize = 150

MyCSB. ConnectionTimeout = 30

Dim myConnection As SqlConnection = New SqlConnection (myCSB. ConnectionString)

Note,In this example, weUseAcceptedConnectionThe string is used as the SqlConnection constructor of the parameter.

OpenConnection
OpenConnection:
[C #]

1

SqlConnection1.Open ();

[Visual Basic]

1

SqlConnection1.Open ()

Of course, SqlConnection1 must have an effective one allocated earlier.ConnectionString. When you open it, dotConnect for SQL Server tries to find the host andConnectionTo the server. If any problem occurs, it will generate a brief description of the error. If dotConnect for SQL Server tries to completeConnectionDuring the ConnectionTimeout interval, no problem occurs. Eventually,ConnectionWill be created, the method will be restored, and the State attribute will be changed to Open.

You can use several steps to link to the server during design:

  1. Right-click the Table DesignerSqlConnection1 object.
  2. SelectConnection.
  3. Provide necessary logon information in the dialog box.
  4. ClickConnectionButton completeConnection.

Or you can simply change the strong> State attributeOpen, With the currentConnectionString to completeConnection.

CloseConnection
Call its close method to closeConnection, Or setStateAttribute to close.
The following example summarizes the above information and demonstratesHowCreate, set, open,UseAnd disableConnection.

[C #]

1

2

3

4

5

SqlConnection myConn = new SqlConnection ();

MyConn. ConnectionString = "User Id = sa; Password = mypassword; DataSource = 127.0.0.1 ;";

MyConn. Open ();

MessageBox. Show (myConn. ServerVersion );

MyConn. Close ();

[Visual Basic]

1

2

3

4

5

Dim myConn As SqlConnection = New SqlConnection ()

MyConn. ConnectionString = "User Id = sa; Password = mypassword; DataSource = 127.0.0.1 ;"

MyConn. Open ()

MessageBox. Show (myConn. ServerVersion)

MyConn. Close ()

Sample CodeConnectionGo to the server, display its version, and then turn it offConnection. This is not commonly used, because in real applicationsConnectionOther such as SqlCommand and SqlDataTableUse.

ModifyConnection
You can modify the attributes of the SqlConnection object by changing the attributes of the object.Connection. Remember, when some attributes can be freely modified, most of them are switched off when new values are assigned.Connection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.