Connector/Net Programming
1897
Even when using the
MySqlHelper
class, a
MySqlConnection
object is created by the helper class.
This section describes how to connect to MySQL using the
MySqlConnection
object.
20.2.5.2. Creating a Connector/Net Connection String
The
MySqlConnection
object is configured using a connection string. A connection string contains
several key/value pairs, separated by semicolons. In each key/value pair, the option name and its
corresponding value are joined by an equal sign. For the list of option names to use in the connection
string, see
Section 20.2.6, “Connector/Net Connection String Options Reference”
.
The following is a sample connection string:
Server=127.0.0.1;Uid=root;Pwd=12345;Database=test;
In this example, the
MySqlConnection
object is configured to connect to a MySQL server at
127.0.0.1
, with a user name of
root
and a password of
12345
. The default database for all
statements will be the
test
database.
Note
Using the '@' symbol for parameters is now the preferred approach, although
the old pattern of using '?' is still supported. To avoid conflicts when using
the '@' symbol in combination with user variables, see the
Allow User
Variables
connection string option in
Section 20.2.6, “Connector/Net
Connection String Options Reference”
. The
Old Syntax
connection string
option has now been deprecated.
20.2.5.2.1. Opening a Connection
Once you have created a connection string it can be used to open a connection to the MySQL server.
The following code is used to create a
MySqlConnection
object, assign the connection string, and
open the connection.
Connector/Net can also connect using the native Windows authentication plugin. See
Section 20.2.5.5,
“Using the Windows Native Authentication Plugin”
for details.
You can further extend the authentication mechanism by writing your own authentication plugin. See
Section 20.2.5.6, “Writing a Custom Authentication Plugin”
for details.
Visual Basic Example
Dim conn As New MySql.Data.MySqlClient.MySqlConnection
Dim myConnectionString as String
myConnectionString = "server=127.0.0.1;" _
& "uid=root;" _
& "pwd=12345;" _
& "database=test;"
Try
conn.ConnectionString = myConnectionString
conn.Open()
Catch ex As MySql.Data.MySqlClient.MySqlException
MessageBox.Show(ex.Message)
End Try
C# Example
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=127.0.0.1;uid=root;" +
"pwd=12345;database=test;";
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...