2 <MyMembers name="Class">
\r
5 A <b>MySqlConnection</b> object represents a session to a MySQL Server
\r
6 data source. When you create an instance of <B>MySqlConnection</B>, all
\r
7 properties are set to their initial values. For a list of these values, see the
\r
8 <B>MySqlConnection</B> constructor.
\r
12 If the <B>MySqlConnection</B> goes out of scope, it is not closed. Therefore,
\r
13 you must explicitly close the connection by calling <see cref="MySqlConnection.Close"/>
\r
14 or <see cref="MySqlConnection.Dispose"/>.
\r
20 The following example creates a <see cref="MySqlCommand"/> and
\r
21 a <B>MySqlConnection</B>. The <B>MySqlConnection</B> is opened and set as the
\r
22 <see cref="MySqlCommand.Connection"/> for the <B>MySqlCommand</B>. The example then calls
\r
23 <see cref="MySqlCommand.ExecuteNonQuery"/>, and closes the connection. To accomplish this, the <B>ExecuteNonQuery</B> is
\r
24 passed a connection string and a query string that is a SQL INSERT
\r
26 <code lang="Visual Basic">
\r
28 Public Sub InsertRow(myConnectionString As String)
\r
29 ' If the connection string is null, use a default.
\r
30 If myConnectionString = "" Then
\r
31 myConnectionString = "Database=Test;Data Source=localhost;User Id=username;Password=pass"
\r
33 Dim myConnection As New MySqlConnection(myConnectionString)
\r
34 Dim myInsertQuery As String = "INSERT INTO Orders (id, customerId, amount) Values(1001, 23, 30.66)"
\r
35 Dim myCommand As New MySqlCommand(myInsertQuery)
\r
36 myCommand.Connection = myConnection
\r
38 myCommand.ExecuteNonQuery()
\r
39 myCommand.Connection.Close()
\r
45 public void InsertRow(string myConnectionString)
\r
47 // If the connection string is null, use a default.
\r
48 if(myConnectionString == "")
\r
50 myConnectionString = "Database=Test;Data Source=localhost;User Id=username;Password=pass";
\r
52 MySqlConnection myConnection = new MySqlConnection(myConnectionString);
\r
53 string myInsertQuery = "INSERT INTO Orders (id, customerId, amount) Values(1001, 23, 30.66)";
\r
54 MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
\r
55 myCommand.Connection = myConnection;
\r
56 myConnection.Open();
\r
57 myCommand.ExecuteNonQuery();
\r
58 myCommand.Connection.Close();
\r
67 <MyMembers name="ConnectionTimeout">
\r
68 <exception cref="System.ArgumentException">The value set is less than 0.</exception>
\r
70 A value of 0 indicates no limit, and should be avoided in a
\r
71 <see cref="MySqlConnection.ConnectionString"/> because an attempt to connect
\r
72 will wait indefinitely.
\r
75 The following example creates a MySqlConnection
\r
76 and sets some of its properties in the connection string.
\r
77 <code lang="Visual Basic">
\r
78 Public Sub CreateSqlConnection()
\r
79 Dim myConnection As New MySqlConnection()
\r
80 myConnection.ConnectionString = "Persist Security Info=False;Username=user;Password=pass;database=test1;server=localhost;Connect Timeout=30"
\r
85 public void CreateSqlConnection()
\r
87 MySqlConnection myConnection = new MySqlConnection();
\r
88 myConnection.ConnectionString = "Persist Security Info=False;Username=user;Password=pass;database=test1;server=localhost;Connect Timeout=30";
\r
89 myConnection.Open();
\r
96 <MyMembers name="ConnectionString">
\r
98 <para>The <B>ConnectionString</B> returned may not be exactly like what was originally
\r
99 set but will be indentical in terms of keyword/value pairs. Security information
\r
100 will not be included unless the Persist Security Info value is set to true.
\r
102 <para>You can use the <B>ConnectionString</B> property to connect to a database.
\r
103 The following example illustrates a typical connection string.</para>
\r
104 <c>"Persist Security Info=False;database=MyDB;server=MySqlServer;user id=myUser;Password=myPass"</c>
\r
105 <para>The <B>ConnectionString</B> property can be set only when the connection is
\r
106 closed. Many of the connection string values have corresponding read-only
\r
107 properties. When the connection string is set, all of these properties are
\r
108 updated, except when an error is detected. In this case, none of the properties
\r
109 are updated. <see cref="MySqlConnection"/> properties return only those settings contained in the
\r
110 <B>ConnectionString</B>.</para>
\r
111 <para>To connect to a local machine, specify "localhost" for the server. If you do not
\r
112 specify a server, localhost is assumed.</para>
\r
113 <para>Resetting the <B>ConnectionString</B> on a closed connection resets all
\r
114 connection string values (and related properties) including the password. For
\r
115 example, if you set a connection string that includes "Database= MyDb", and
\r
116 then reset the connection string to "Data Source=myserver;User Id=myUser;Password=myPass",
\r
117 the <see cref="MySqlConnection.Database"/> property is no longer set to MyDb.</para>
\r
118 <para>The connection string is parsed immediately after being set. If errors in
\r
119 syntax are found when parsing, a runtime exception, such as <see cref="ArgumentException"/>,
\r
120 is generated. Other errors can be found only when an attempt is made to open the
\r
122 <para>The basic format of a connection string consists of a series of keyword/value
\r
123 pairs separated by semicolons. The equal sign (=) connects each keyword and its
\r
124 value. To include values that contain a semicolon, single-quote character, or
\r
125 double-quote character, the value must be enclosed in double quotes. If the
\r
126 value contains both a semicolon and a double-quote character, the value can be
\r
127 enclosed in single quotes. The single quote is also useful if the value begins
\r
128 with a double-quote character. Conversely, the double quote can be used if the
\r
129 value begins with a single quote. If the value contains both single-quote and
\r
130 double-quote characters, the quote character used to enclose the value must be
\r
131 doubled each time it occurs within the value.</para>
\r
132 <para>To include preceding or trailing spaces in the string value, the value must
\r
133 be enclosed in either single quotes or double quotes. Any leading or trailing
\r
134 spaces around integer, Boolean, or enumerated values are ignored, even if
\r
135 enclosed in quotes. However, spaces within a string literal keyword or value are
\r
136 preserved. Using .NET Framework version 1.1, single or double quotes may be used
\r
137 within a connection string without using delimiters (for example, Data Source=
\r
138 my'Server or Data Source= my"Server), unless a quote character is the first or
\r
139 last character in the value.</para>
\r
140 <para>To include an equal sign (=) in a keyword or value, it must be preceded by
\r
141 another equal sign. For example, in the hypothetical connection string</para>
\r
142 <c>"key==word=value"</c>
\r
144 <para>the keyword is "key=word" and the value is "value".</para>
\r
145 <para>If a specific keyword in a keyword= value pair occurs multiple times in a
\r
146 connection string, the last occurrence listed is used in the value set.</para>
\r
147 <para>Keywords are not case sensitive.</para>
\r
148 <para>The following table lists the valid names for keyword values within the
\r
149 <B>ConnectionString</B>.</para>
\r
150 <div class="tablediv"><table class="dtTABLE" cellspacing="0"><tr valign="top">
\r
151 <th width="33%">Name</th>
\r
152 <th width="33%">Default</th>
\r
153 <th width="33%">Description</th>
\r
156 <td>Connect Timeout<para> -or- </para>Connection Timeout</td>
\r
158 <td>The length of time (in seconds) to wait for a connection to the server before
\r
159 terminating the attempt and generating an error.</td>
\r
163 Host<para> -or- </para>Server<para> -or- </para>Data Source<para> -or- </para>
\r
164 DataSource<para> -or- </para>Address<para> -or- </para>Addr<para> -or- </para>
\r
168 <td><para>The name or network address of the instance of MySQL to which to connect. Multiple hosts can be
\r
169 specified separated by &. This can be useful where multiple MySQL servers are configured for replication
\r
170 and you are not concerned about the precise server you are connecting to. No attempt is made by the provider to
\r
171 synchronize writes to the database so care should be taken when using this option.
\r
174 In Unix environment with Mono, this can be a fully qualified path to MySQL socket filename. With this configuration, the Unix socket will be used instead of TCP/IP socket.
\r
175 Currently only a single socket name can be given so accessing MySQL in a replicated environment using Unix sockets is not currently supported.
\r
182 <td>The port MySQL is using to listen for connections. Specify -1 for this value to use a
\r
183 named pipe connection (Windows only). This value is ignored if Unix socket is used.</td>
\r
188 <td>When <B>true</B>, SSL encryption is used for all data sent between the
\r
189 client and server if the server has a certificate installed. Recognized values
\r
190 are <B>true</B>, <B>false</B>, <B>yes</B>, and <B>no</B>.<para><b>Note</b> This parameter currently has no
\r
191 effect.</para></td>
\r
194 <td>Initial Catalog<para> -or- </para>Database</td>
\r
196 <td>The name of the database to use intially</td>
\r
199 <td>Password<para> -or- </para>pwd</td>
\r
201 <td>The password for the MySQL account being used.</td>
\r
204 <td>Persist Security Info</td>
\r
206 <td>When set to <B>false</B> or <B>no</B> (strongly recommended), security-sensitive
\r
207 information, such as the password, is not returned as part of the connection if
\r
208 the connection is open or has ever been in an open state. Resetting the
\r
209 connection string resets all connection string values including the password.
\r
210 Recognized values are <B>true</B>, <B>false</B>, <B>yes</B>, and <B>no</B>.</td>
\r
213 <td>User Id<para> -or- </para>Username<para> -or- </para>Uid<para> -or- </para>User name</td>
\r
215 <td>The MySQL login account being used.</td>
\r
220 The following table lists the valid names for connection pooling values within
\r
221 the <B>ConnectionString</B>. For more information about connection pooling, see
\r
222 Connection Pooling for the ByteFX Data Provider for MySQL.</para>
\r
223 <div class="tablediv"><table class="dtTABLE" cellspacing="0"><tr valign="top">
\r
224 <th width="33%">Name</th>
\r
225 <th width="33%">Default</th>
\r
226 <th width="33%">Description</th>
\r
229 <td>Connection Lifetime</td>
\r
231 <td>When a connection is returned to the pool, its creation time is compared with
\r
232 the current time, and the connection is destroyed if that time span (in seconds)
\r
233 exceeds the value specified by <B>Connection Lifetime</B>. This is useful in
\r
234 clustered configurations to force load balancing between a running server and a
\r
235 server just brought online.
\r
236 <para>A value of zero (0) causes pooled connections to have the maximum connection
\r
237 timeout.</para></td>
\r
240 <td>Max Pool Size</td>
\r
242 <td>The maximum number of connections allowed in the pool.</td>
\r
245 <td>Min Pool Size</td>
\r
247 <td>The minimum number of connections allowed in the pool.</td>
\r
252 <td>When <B>true</B>, the <B>MySqlConnection</B> object is drawn from the appropriate
\r
253 pool, or if necessary, is created and added to the appropriate pool. Recognized
\r
254 values are <B>true</B>, <B>false</B>, <B>yes</B>, and <B>no</B>.</td>
\r
257 <td>Use Pipe<para> -or- </para>Pipe</td>
\r
259 <td>When set to the name of a named pipe, the <B>MySqlConnection</B> will attempt to connect to MySQL
\r
260 on that named pipe.<br/><br/>This settings only applies to the Windows platform.</td>
\r
263 <para>When setting keyword or connection pooling values that require a Boolean
\r
264 value, you can use 'yes' instead of 'true', and 'no' instead of 'false'.</para>
\r
265 <para><B>Note</B> The ByteFX Data Provider for MySQL uses the native socket protocol to
\r
266 communicate with MySQL. Therefore, it does not support the use of an ODBC data source name (DSN) when
\r
267 connecting to MySQL because it does not add an ODBC layer.</para>
\r
268 <para><B>CAUTION</B> In this release, the application should use caution when constructing a
\r
269 connection string based on user input (for example when retrieving user ID and password information from a
\r
270 dialog box, and appending it to the connection string). The application should
\r
271 ensure that a user cannot embed extra connection string parameters in these
\r
272 values (for example, entering a password as "validpassword;database=somedb" in
\r
273 an attempt to attach to a different database).</para>
\r
276 The following example creates a <see cref="MySqlConnection"/> and sets some of its properties
\r
277 <code lang="Visual Basic">
\r
278 Public Sub CreateConnection()
\r
279 Dim myConnection As New MySqlConnection()
\r
280 myConnection.ConnectionString = "Persist Security Info=False;database=myDB;server=myHost;Connect Timeout=30;user id=myUser; pwd=myPass"
\r
281 myConnection.Open()
\r
282 End Sub 'CreateConnection
\r
285 public void CreateConnection()
\r
287 MySqlConnection myConnection = new MySqlConnection();
\r
288 myConnection.ConnectionString = "Persist Security Info=False;database=myDB;server=myHost;Connect Timeout=30;user id=myUser; pwd=myPass";
\r
289 myConnection.Open();
\r
294 The following example creates a <see cref="MySqlConnection"/> in Unix environment with Mono installed. MySQL socket filename used in this example is "/var/lib/mysql/mysql.sock". The actual filename depends on your MySQL configuration.
\r
295 <code lang="Visual Basic">
\r
296 Public Sub CreateConnection()
\r
297 Dim myConnection As New MySqlConnection()
\r
298 myConnection.ConnectionString = "database=myDB;server=/var/lib/mysql/mysql.sock;user id=myUser; pwd=myPass"
\r
299 myConnection.Open()
\r
300 End Sub 'CreateConnection
\r
303 public void CreateConnection()
\r
305 MySqlConnection myConnection = new MySqlConnection();
\r
306 myConnection.ConnectionString = "database=myDB;server=/var/lib/mysql/mysql.sock;user id=myUser; pwd=myPass";
\r
307 myConnection.Open();
\r