byte
) but nor does it guarantee
+ that all operations will work in such cases.
+
+ [root] -+- foo -+- bar
+ | |
+ | +- baz
+ |
+ +- bar --- baz
+
+
+
+ This delegates most behavior to the
+
+ Most applications will never need to create their own instances of this type;
+ instead, use the static
+ Public Sub InsertRow(myConnectionString As String)
+ " If the connection string is null, use a default.
+ If myConnectionString = "" Then
+ myConnectionString = "Database=Test;Data Source=localhost;User Id=username;Password=pass"
+ End If
+
+ Dim myConnection As New MySqlConnection(myConnectionString)
+ Dim myInsertQuery As String = "INSERT INTO Orders (id, customerId, amount) Values(1001, 23, 30.66)"
+ Dim myCommand As New MySqlCommand(myInsertQuery)
+ myCommand.Connection = myConnection
+ myConnection.Open()
+ myCommand.ExecuteNonQuery()
+ myCommand.Connection.Close()
+ End Sub
+
+
+ public void InsertRow(string myConnectionString)
+ {
+ // If the connection string is null, use a default.
+ if(myConnectionString == "")
+ {
+ myConnectionString = "Database=Test;Data Source=localhost;User Id=username;Password=pass";
+ }
+
+ MySqlConnection myConnection = new MySqlConnection(myConnectionString);
+ string myInsertQuery = "INSERT INTO Orders (id, customerId, amount) Values(1001, 23, 30.66)";
+ MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
+ myCommand.Connection = myConnection;
+ myConnection.Open();
+ myCommand.ExecuteNonQuery();
+ myCommand.Connection.Close();
+ }
+
+
+ Public Sub CreateMySqlCommand()
+ Dim myConnection As New MySqlConnection _
+ ("Persist Security Info=False;database=test;server=myServer")
+ myConnection.Open()
+ Dim myTrans As MySqlTransaction = myConnection.BeginTransaction()
+ Dim mySelectQuery As String = "SELECT * FROM MyTable"
+ Dim myCommand As New MySqlCommand(mySelectQuery, myConnection, myTrans)
+ myCommand.CommandTimeout = 20
+ End Sub
+
+
+ public void CreateMySqlCommand()
+ {
+ MySqlConnection myConnection = new MySqlConnection("Persist Security Info=False;database=test;server=myServer");
+ myConnection.Open();
+ MySqlTransaction myTrans = myConnection.BeginTransaction();
+ string mySelectQuery = "SELECT * FROM myTable";
+ MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection,myTrans);
+ myCommand.CommandTimeout = 20;
+ }
+
+
+ Public Sub CreateMySqlCommand()
+ Dim myCommand As New MySqlCommand()
+ myCommand.CommandType = CommandType.Text
+ End Sub
+
+
+ public void CreateMySqlCommand()
+ {
+ MySqlCommand myCommand = new MySqlCommand();
+ myCommand.CommandType = CommandType.Text;
+ }
+
+
+ Public Sub CreateMySqlCommand()
+ Dim sql as String = "SELECT * FROM mytable"
+ Dim myCommand As New MySqlCommand(sql)
+ myCommand.CommandType = CommandType.Text
+ End Sub
+
+
+ public void CreateMySqlCommand()
+ {
+ string sql = "SELECT * FROM mytable";
+ MySqlCommand myCommand = new MySqlCommand(sql);
+ myCommand.CommandType = CommandType.Text;
+ }
+
+
+ Public Sub CreateMySqlCommand()
+ Dim conn as new MySqlConnection("server=myServer")
+ Dim sql as String = "SELECT * FROM mytable"
+ Dim myCommand As New MySqlCommand(sql, conn)
+ myCommand.CommandType = CommandType.Text
+ End Sub
+
+
+ public void CreateMySqlCommand()
+ {
+ MySqlConnection conn = new MySqlConnection("server=myserver")
+ string sql = "SELECT * FROM mytable";
+ MySqlCommand myCommand = new MySqlCommand(sql, conn);
+ myCommand.CommandType = CommandType.Text;
+ }
+
+
+ Public Sub CreateMySqlCommand()
+ Dim conn as new MySqlConnection("server=myServer")
+ conn.Open();
+ Dim txn as MySqlTransaction = conn.BeginTransaction()
+ Dim sql as String = "SELECT * FROM mytable"
+ Dim myCommand As New MySqlCommand(sql, conn, txn)
+ myCommand.CommandType = CommandType.Text
+ End Sub
+
+
+ public void CreateMySqlCommand()
+ {
+ MySqlConnection conn = new MySqlConnection("server=myserver")
+ conn.Open();
+ MySqlTransaction txn = conn.BeginTransaction();
+ string sql = "SELECT * FROM mytable";
+ MySqlCommand myCommand = new MySqlCommand(sql, conn, txn);
+ myCommand.CommandType = CommandType.Text;
+ }
+
+
+ Public Sub CreateMySqlCommand()
+ Dim myCommand As New MySqlCommand()
+ myCommand.CommandText = "SELECT * FROM Mytable ORDER BY id"
+ myCommand.CommandType = CommandType.Text
+ End Sub
+
+
+ public void CreateMySqlCommand()
+ {
+ MySqlCommand myCommand = new MySqlCommand();
+ myCommand.CommandText = "SELECT * FROM mytable ORDER BY id";
+ myCommand.CommandType = CommandType.Text;
+ }
+
+
+ Public Sub CreateMySqlCommand()
+ Dim myCommand As New MySqlCommand()
+ myCommand.CommandType = CommandType.Text
+ End Sub
+
+
+ public void CreateMySqlCommand()
+ {
+ MySqlCommand myCommand = new MySqlCommand();
+ myCommand.CommandType = CommandType.Text;
+ }
+
+
+ Public Sub CreateMySqlCommand()
+ Dim mySelectQuery As String = "SELECT * FROM mytable ORDER BY id"
+ Dim myConnectString As String = "Persist Security Info=False;database=test;server=myServer"
+ Dim myCommand As New MySqlCommand(mySelectQuery)
+ myCommand.Connection = New MySqlConnection(myConnectString)
+ myCommand.CommandType = CommandType.Text
+ End Sub
+
+
+ public void CreateMySqlCommand()
+ {
+ string mySelectQuery = "SELECT * FROM mytable ORDER BY id";
+ string myConnectString = "Persist Security Info=False;database=test;server=myServer";
+ MySqlCommand myCommand = new MySqlCommand(mySelectQuery);
+ myCommand.Connection = new MySqlConnection(myConnectString);
+ myCommand.CommandType = CommandType.Text;
+ }
+
+
+ Public Sub CreateMySqlCommand(myConnection As MySqlConnection, _
+ mySelectQuery As String, myParamArray() As MySqlParameter)
+ Dim myCommand As New MySqlCommand(mySelectQuery, myConnection)
+ myCommand.CommandText = "SELECT id, name FROM mytable WHERE age=@age"
+ myCommand.UpdatedRowSource = UpdateRowSource.Both
+ myCommand.Parameters.Add(myParamArray)
+ Dim j As Integer
+ For j = 0 To myCommand.Parameters.Count - 1
+ myCommand.Parameters.Add(myParamArray(j))
+ Next j
+ Dim myMessage As String = ""
+ Dim i As Integer
+ For i = 0 To myCommand.Parameters.Count - 1
+ myMessage += myCommand.Parameters(i).ToString() & ControlChars.Cr
+ Next i
+ Console.WriteLine(myMessage)
+ End Sub
+
+
+ public void CreateMySqlCommand(MySqlConnection myConnection, string mySelectQuery,
+ MySqlParameter[] myParamArray)
+ {
+ MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
+ myCommand.CommandText = "SELECT id, name FROM mytable WHERE age=@age";
+ myCommand.Parameters.Add(myParamArray);
+ for (int j=0; j<myParamArray.Length; j++)
+ {
+ myCommand.Parameters.Add(myParamArray[j]) ;
+ }
+ string myMessage = "";
+ for (int i = 0; i < myCommand.Parameters.Count; i++)
+ {
+ myMessage += myCommand.Parameters[i].ToString() + "\n";
+ }
+ MessageBox.Show(myMessage);
+ }
+
+
+ Public Sub CreateMySqlCommandWithQueryAttributes(ByVal myConnection As MySqlConnection)
+ Dim myCommand As MySqlCommand = New MySqlCommand()
+ myCommand.Connection = myConnection
+ Dim mySqlAttribute As MySqlAttribute = New MySqlAttribute("qa1", "qaValue")
+ Dim mySqlAttribute2 As MySqlAttribute = New MySqlAttribute("qa2", 2)
+ myCommand.Attributes.SetAttribute(mySqlAttribute)
+ myCommand.Attributes.SetAttribute(mySqlAttribute2)
+ myCommand.CommandText = $"SELECT mysql_query_attribute_string('{mySqlAttribute.AttributeName}') AS attr1," &
+ $"mysql_query_attribute_string('{mySqlAttribute2.AttributeName}') AS attr2"
+
+ Using reader = myCommand.ExecuteReader()
+ While reader.Read()
+ Console.WriteLine($"Attribute1 Value: {reader.GetString(0)}")
+ Console.WriteLine($"Attribute1 Value: {reader.GetString(1)}")
+ End While
+ End Using
+ End Sub
+
+
+ public void CreateMySqlCommandWithQueryAttributes(MySqlConnection myConnection)
+ {
+ MySqlCommand myCommand = new MySqlCommand();
+ myCommand.Connection = myConnection;
+
+ MySqlAttribute mySqlAttribute = new MySqlAttribute("qa1", "qaValue");
+ MySqlAttribute mySqlAttribute2 = new MySqlAttribute("qa2", 2);
+
+ myCommand.Attributes.SetAttribute(mySqlAttribute);
+ myCommand.Attributes.SetAttribute(mySqlAttribute2);
+
+ myCommand.CommandText = $"SELECT mysql_query_attribute_string('{mySqlAttribute.AttributeName}') AS attr1," +
+ $"mysql_query_attribute_string('{mySqlAttribute2.AttributeName}') AS attr2";
+
+ using (var reader = myCommand.ExecuteReader())
+ {
+ while (reader.Read())
+ {
+ Console.WriteLine($"Attribute1 Value: {reader.GetString(0)}");
+ Console.WriteLine($"Attribute1 Value: {reader.GetString(1)}");
+ }
+ }
+ }
+
+
+ Public Sub CreateMySqlCommand(myExecuteQuery As String, myConnection As MySqlConnection)
+ Dim myCommand As New MySqlCommand(myExecuteQuery, myConnection)
+ myCommand.Connection.Open()
+ myCommand.ExecuteNonQuery()
+ myConnection.Close()
+ End Sub
+
+
+ public void CreateMySqlCommand(string myExecuteQuery, MySqlConnection myConnection)
+ {
+ MySqlCommand myCommand = new MySqlCommand(myExecuteQuery, myConnection);
+ myCommand.Connection.Open();
+ myCommand.ExecuteNonQuery();
+ myConnection.Close();
+ }
+
+
+ Public Sub CreateMySqlDataReader(mySelectQuery As String, myConnection As MySqlConnection)
+ Dim myCommand As New MySqlCommand(mySelectQuery, myConnection)
+ myConnection.Open()
+ Dim myReader As MySqlDataReader
+ myReader = myCommand.ExecuteReader()
+
+ Try
+ While myReader.Read()
+ Console.WriteLine(myReader.GetString(0))
+ End While
+ Finally
+ myReader.Close
+ myConnection.Close
+ End Try
+ End Sub
+
+
+ public void CreateMySqlDataReader(string mySelectQuery, MySqlConnection myConnection)
+ {
+ MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
+ myConnection.Open();
+ MMySqlDataReader myReader;
+ myReader = myCommand.ExecuteReader();
+ try
+ {
+ while(myReader.Read())
+ {
+ Console.WriteLine(myReader.GetString(0));
+ }
+ }
+ finally
+ {
+ myReader.Close();
+ myConnection.Close();
+ }
+ }
+
+
+ Public Sub CreateMySqlCommand(myScalarQuery As String, myConnection As MySqlConnection)
+ Dim myCommand As New MySqlCommand(myScalarQuery, myConnection)
+ myCommand.Connection.Open()
+ myCommand.ExecuteScalar()
+ myConnection.Close()
+ End Sub
+
+
+ public void CreateMySqlCommand(string myScalarQuery, MySqlConnection myConnection)
+ {
+ MySqlCommand myCommand = new MySqlCommand(myScalarQuery, myConnection);
+ myCommand.Connection.Open();
+ myCommand.ExecuteScalar();
+ myConnection.Close();
+ }
+
+
+ public sub PrepareExample()
+ Dim cmd as New MySqlCommand("INSERT INTO mytable VALUES (@val)", myConnection)
+ cmd.Parameters.Add( "@val", 10 )
+ cmd.Prepare()
+ cmd.ExecuteNonQuery()
+
+ cmd.Parameters(0).Value = 20
+ cmd.ExecuteNonQuery()
+ end sub
+
+
+ private void PrepareExample()
+ {
+ MySqlCommand cmd = new MySqlCommand("INSERT INTO mytable VALUES (@val)", myConnection);
+ cmd.Parameters.Add( "@val", 10 );
+ cmd.Prepare();
+ cmd.ExecuteNonQuery();
+
+ cmd.Parameters[0].Value = 20;
+ cmd.ExecuteNonQuery();
+ }
+
+
+
+ Public Sub InsertRow(myConnectionString As String)
+ ' If the connection string is null, use a default.
+ If myConnectionString = "" Then
+ myConnectionString = "Database=Test;Data Source=localhost;User Id=username;Password=pass"
+ End If
+ Dim myConnection As New MySqlConnection(myConnectionString)
+ Dim myInsertQuery As String = "INSERT INTO Orders (id, customerId, amount) Values(1001, 23, 30.66)"
+ Dim myCommand As New MySqlCommand(myInsertQuery)
+ myCommand.Connection = myConnection
+ myConnection.Open()
+ myCommand.ExecuteNonQuery()
+ myCommand.Connection.Close()
+ End Sub
+
+
+
+
+ public void InsertRow(string myConnectionString)
+ {
+ // If the connection string is null, use a default.
+ if(myConnectionString == "")
+ {
+ myConnectionString = "Database=Test;Data Source=localhost;User Id=username;Password=pass";
+ }
+ MySqlConnection myConnection = new MySqlConnection(myConnectionString);
+ string myInsertQuery = "INSERT INTO Orders (id, customerId, amount) Values(1001, 23, 30.66)";
+ MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
+ myCommand.Connection = myConnection;
+ myConnection.Open();
+ myCommand.ExecuteNonQuery();
+ myCommand.Connection.Close();
+ }
+
+
+
+ Public Sub CreateSqlConnection()
+ Dim myConnection As New MySqlConnection()
+ myConnection.ConnectionString = "Persist Security Info=False;Username=user;Password=pass;database=test1;server=localhost;Connect Timeout=30"
+ myConnection.Open()
+ End Sub
+
+
+ public void CreateSqlConnection()
+ {
+ MySqlConnection myConnection = new MySqlConnection();
+ myConnection.ConnectionString = "Persist Security Info=False;Username=user;Password=pass;database=test1;server=localhost;Connect Timeout=30";
+ myConnection.Open();
+ }
+
+
+ Public Sub CreateMySqlConnection()
+ Dim myConnString As String = _
+ "Persist Security Info=False;database=test;server=localhost;user id=joeuser;pwd=pass"
+ Dim myConnection As New MySqlConnection( myConnString )
+ myConnection.Open()
+ MessageBox.Show( "Server Version: " + myConnection.ServerVersion _
+ + ControlChars.NewLine + "Database: " + myConnection.Database )
+ myConnection.ChangeDatabase( "test2" )
+ MessageBox.Show( "ServerVersion: " + myConnection.ServerVersion _
+ + ControlChars.NewLine + "Database: " + myConnection.Database )
+ myConnection.Close()
+ End Sub
+
+
+
+ public void CreateMySqlConnection()
+ {
+ string myConnString =
+ "Persist Security Info=False;database=test;server=localhost;user id=joeuser;pwd=pass";
+ MySqlConnection myConnection = new MySqlConnection( myConnString );
+ myConnection.Open();
+ MessageBox.Show( "Server Version: " + myConnection.ServerVersion
+ + "\nDatabase: " + myConnection.Database );
+ myConnection.ChangeDatabase( "test2" );
+ MessageBox.Show( "ServerVersion: " + myConnection.ServerVersion
+ + "\nDatabase: " + myConnection.Database );
+ myConnection.Close();
+ }
+
+
+ Public Sub CreateMySqlConnection(myConnString As String)
+ Dim myConnection As New MySqlConnection(myConnString)
+ myConnection.Open()
+ MessageBox.Show("ServerVersion: " + myConnection.ServerVersion _
+ + ControlChars.Cr + "State: " + myConnection.State.ToString())
+ myConnection.Close()
+ End Sub
+
+
+ public void CreateMySqlConnection(string myConnString)
+ {
+ MySqlConnection myConnection = new MySqlConnection(myConnString);
+ myConnection.Open();
+ MessageBox.Show("ServerVersion: " + myConnection.ServerVersion +
+ "\nState: " + myConnection.State.ToString());
+ myConnection.Close();
+ }
+
+
+ Public Sub CreateMySqlConnection(myConnString As String)
+ Dim myConnection As New MySqlConnection(myConnString)
+ myConnection.Open()
+ MessageBox.Show("ServerVersion: " + myConnection.ServerVersion _
+ + ControlChars.Cr + "State: " + myConnection.State.ToString())
+ myConnection.Close()
+ End Sub
+
+
+ public void CreateMySqlConnection(string myConnString)
+ {
+ MySqlConnection myConnection = new MySqlConnection(myConnString);
+ myConnection.Open();
+ MessageBox.Show("ServerVersion: " + myConnection.ServerVersion +
+ "\nState: " + myConnection.State.ToString());
+ myConnection.Close();
+ }
+
+ Name | +Default | +Description | +
---|---|---|
+ Connect Timeout |
+ 15 | ++ The length of time (in seconds) to wait for a connection to the server before + terminating the attempt and generating an error. + | +
+ Host |
+ localhost | +
+ |
+
Port | +3306 | ++ The port MySQL is using to listen for connections. This value is ignored if the connection protocol + is anything but socket. + | +
Protocol | +socket | +
+ Specifies the type of connection to make to the server. + pipe for a named pipe connection + unix for a Unix socket connection + memory to use MySQL shared memory + |
+
+ CharSet |
+ + | + Specifies the character set that should be used to encode all queries sent to the server. + Resultsets are still returned in the character set of the data returned. + | +
Logging | +false | +When true, various pieces of information is output to any configured TraceListeners. | +
Allow Batch | +true | +
+ When true, multiple SQL statements can be sent with one command execution. + -Note- + Starting with MySQL 4.1.1, batch statements should be separated by the server-defined seperator character. + Commands sent to earlier versions of MySQL should be seperated with ';'. + |
+
Encrypt | +false | ++ When true, SSL/TLS encryption is used for all data sent between the + client and server if the server has a certificate installed. Recognized values + are true, false, yes, and no. + | +
+ Initial Catalog |
+ mysql | +The name of the database to use intially | +
+ Password |
+ + | The password for the MySQL account being used. | +
Persist Security Info | +false | ++ When set to false or no (strongly recommended), security-sensitive + information, such as the password, is not returned as part of the connection if + the connection is open or has ever been in an open state. Resetting the + connection string resets all connection string values including the password. + Recognized values are true, false, yes, and no. + | +
+ User Id |
+ + | The MySQL login account being used. | +
Shared Memory Name | +MYSQL | +The name of the shared memory object to use for communication if the connection protocol is set to memory. | +
Allow Zero Datetime | +false | ++ True to have MySqlDataReader.GetValue() return a MySqlDateTime for date or datetime columns that have illegal values. + False will cause a DateTime object to be returned for legal values and an exception will be thrown for illegal values. + | +
Convert Zero Datetime | +false | ++ True to have MySqlDataReader.GetValue() and MySqlDataReader.GetDateTime() + return DateTime.MinValue for date or datetime columns that have illegal values. + | +
+ Pipe Name |
+ mysql | +
+ When set to the name of a named pipe, the MySqlConnection will attempt to connect to MySQL
+ on that named pipe. This settings only applies to the Windows platform. + |
+
+ Use Performance Monitor |
+ false | ++ Posts performance data that can be tracked using perfmon + | +
+ Procedure Cache Size + | +25 | ++ How many stored procedure definitions can be held in the cache + | +
Use Procedure Bodies | +true | ++ Instructs the provider to attempt to call the procedure without first resolving the metadata. This + is useful in situations where the calling user does not have access to the mysql.proc table. To + use this mode, the parameters for the procedure must be added to the command in the same order + as they appear in the procedure definition and their types must be explicitly set. + | +
Auto Enlist | +true | ++ Indicates whether the connection should automatically enlist in the current transaction, + if there is one. + | +
Respect Binary Flags | +true | ++ Indicates whether the connection should respect all binary flags sent to the client + as part of column metadata. False will cause the connector to behave like + Connector/NET 5.0 and earlier. + | +
BlobAsUTF8IncludePattern | +null | ++ Pattern that should be used to indicate which blob columns should be treated as UTF-8. + | +
BlobAsUTF8ExcludePattern | +null | ++ Pattern that should be used to indicate which blob columns should not be treated as UTF-8. + | +
Default Command Timeout | +30 | ++ The default timeout that new MySqlCommand objects will use unless changed. + | +
Allow User Variables | +false | ++ Should the provider expect user variables in the SQL. + | +
Interactive -or- Interactive Session | +false | ++ Should this session be considered interactive? + | +
Functions Return String | +false | ++ Set this option to true to force the return value of SQL functions to be string. + | +
Use Affected Rows | +false | ++ Set this option to true to cause the affected rows reported to reflect only the + rows that are actually changed. By default, the number of rows that are matched + is returned. + | +
Name | +Default | +Description | +
---|---|---|
Connection Lifetime | +0 | +
+ When a connection is returned to the pool, its creation time is compared with
+ the current time, and the connection is destroyed if that time span (in seconds)
+ exceeds the value specified by Connection Lifetime. This is useful in
+ clustered configurations to force load balancing between a running server and a
+ server just brought online.
+ |
+
Max Pool Size | +100 | +The maximum number of connections allowed in the pool. | +
Min Pool Size | +0 | +The minimum number of connections allowed in the pool. | +
Pooling | +true | ++ When true, the MySqlConnection object is drawn from the appropriate + pool, or if necessary, is created and added to the appropriate pool. Recognized + values are true, false, yes, and no. + | +
Connection Reset | +false | ++ Specifies whether the database connection should be reset when being + drawn from the pool. Leaving this as false will yeild much faster + connection opens but the user should understand the side effects + of doing this such as temporary tables and user variables from the previous + session not being cleared out. + | +
Cache Server Properties | +false | ++ Specifies whether the server variables are cached between pooled connections. + On systems where the variables change infrequently and there are lots of + connection attempts, this can speed up things dramatically. + | +
+ Public Sub CreateConnection()
+ Dim myConnection As New MySqlConnection()
+ myConnection.ConnectionString = "Persist Security Info=False;database=myDB;server=myHost;Connect Timeout=30;user id=myUser; pwd=myPass"
+ myConnection.Open()
+ End Sub 'CreateConnection
+
+
+ public void CreateConnection()
+ {
+ MySqlConnection myConnection = new MySqlConnection();
+ myConnection.ConnectionString = "Persist Security Info=False;database=myDB;server=myHost;Connect Timeout=30;user id=myUser; pwd=myPass";
+ myConnection.Open();
+ }
+
+
+ Public Sub CreateConnection()
+ Dim myConnection As New MySqlConnection()
+ myConnection.ConnectionString = "database=myDB;server=/var/lib/mysql/mysql.sock;user id=myUser; pwd=myPass"
+ myConnection.Open()
+ End Sub 'CreateConnection
+
+
+ public void CreateConnection()
+ {
+ MySqlConnection myConnection = new MySqlConnection();
+ myConnection.ConnectionString = "database=myDB;server=/var/lib/mysql/mysql.sock;user id=myUser; pwd=myPass";
+ myConnection.Open();
+ }
+
+
+ Public Sub RunTransaction(myConnString As String)
+ Dim myConnection As New MySqlConnection(myConnString)
+ myConnection.Open()
+
+ Dim myCommand As MySqlCommand = myConnection.CreateCommand()
+ Dim myTrans As MySqlTransaction
+
+ ' Start a local transaction
+ myTrans = myConnection.BeginTransaction()
+ ' Must assign both transaction object and connection
+ ' to Command object for a pending local transaction
+ myCommand.Connection = myConnection
+ myCommand.Transaction = myTrans
+
+ Try
+ myCommand.CommandText = "Insert into Test (id, desc) VALUES (100, 'Description')"
+ myCommand.ExecuteNonQuery()
+ myCommand.CommandText = "Insert into Test (id, desc) VALUES (101, 'Description')"
+ myCommand.ExecuteNonQuery()
+ myTrans.Commit()
+ Console.WriteLine("Both records are written to database.")
+ Catch e As Exception
+ Try
+ myTrans.Rollback()
+ Catch ex As MySqlException
+ If Not myTrans.Connection Is Nothing Then
+ Console.WriteLine("An exception of type " + ex.GetType().ToString() + _
+ " was encountered while attempting to roll back the transaction.")
+ End If
+ End Try
+
+ Console.WriteLine("An exception of type " + e.GetType().ToString() + _
+ "was encountered while inserting the data.")
+ Console.WriteLine("Neither record was written to database.")
+ Finally
+ myConnection.Close()
+ End Try
+ End Sub
+
+
+ public void RunTransaction(string myConnString)
+ {
+ MySqlConnection myConnection = new MySqlConnection(myConnString);
+ myConnection.Open();
+
+ MySqlCommand myCommand = myConnection.CreateCommand();
+ MySqlTransaction myTrans;
+
+ // Start a local transaction
+ myTrans = myConnection.BeginTransaction();
+ // Must assign both transaction object and connection
+ // to Command object for a pending local transaction
+ myCommand.Connection = myConnection;
+ myCommand.Transaction = myTrans;
+
+ try
+ {
+ myCommand.CommandText = "insert into Test (id, desc) VALUES (100, 'Description')";
+ myCommand.ExecuteNonQuery();
+ myCommand.CommandText = "insert into Test (id, desc) VALUES (101, 'Description')";
+ myCommand.ExecuteNonQuery();
+ myTrans.Commit();
+ Console.WriteLine("Both records are written to database.");
+ }
+ catch(Exception e)
+ {
+ try
+ {
+ myTrans.Rollback();
+ }
+ catch (SqlException ex)
+ {
+ if (myTrans.Connection != null)
+ {
+ Console.WriteLine("An exception of type " + ex.GetType() +
+ " was encountered while attempting to roll back the transaction.");
+ }
+ }
+
+ Console.WriteLine("An exception of type " + e.GetType() +
+ " was encountered while inserting the data.");
+ Console.WriteLine("Neither record was written to database.");
+ }
+ finally
+ {
+ myConnection.Close();
+ }
+ }
+
+
+ Public Sub RunTransaction(myConnString As String)
+ Dim myConnection As New MySqlConnection(myConnString)
+ myConnection.Open()
+
+ Dim myCommand As MySqlCommand = myConnection.CreateCommand()
+ Dim myTrans As MySqlTransaction
+
+ ' Start a local transaction
+ myTrans = myConnection.BeginTransaction()
+ ' Must assign both transaction object and connection
+ ' to Command object for a pending local transaction
+ myCommand.Connection = myConnection
+ myCommand.Transaction = myTrans
+
+ Try
+ myCommand.CommandText = "Insert into Test (id, desc) VALUES (100, 'Description')"
+ myCommand.ExecuteNonQuery()
+ myCommand.CommandText = "Insert into Test (id, desc) VALUES (101, 'Description')"
+ myCommand.ExecuteNonQuery()
+ myTrans.Commit()
+ Console.WriteLine("Both records are written to database.")
+ Catch e As Exception
+ Try
+ myTrans.Rollback()
+ Catch ex As MySqlException
+ If Not myTrans.Connection Is Nothing Then
+ Console.WriteLine("An exception of type " + ex.GetType().ToString() + _
+ " was encountered while attempting to roll back the transaction.")
+ End If
+ End Try
+
+ Console.WriteLine("An exception of type " + e.GetType().ToString() + _
+ "was encountered while inserting the data.")
+ Console.WriteLine("Neither record was written to database.")
+ Finally
+ myConnection.Close()
+ End Try
+ End Sub
+
+
+ public void RunTransaction(string myConnString)
+ {
+ MySqlConnection myConnection = new MySqlConnection(myConnString);
+ myConnection.Open();
+
+ MySqlCommand myCommand = myConnection.CreateCommand();
+ MySqlTransaction myTrans;
+
+ // Start a local transaction
+ myTrans = myConnection.BeginTransaction();
+ // Must assign both transaction object and connection
+ // to Command object for a pending local transaction
+ myCommand.Connection = myConnection;
+ myCommand.Transaction = myTrans;
+
+ try
+ {
+ myCommand.CommandText = "insert into Test (id, desc) VALUES (100, 'Description')";
+ myCommand.ExecuteNonQuery();
+ myCommand.CommandText = "insert into Test (id, desc) VALUES (101, 'Description')";
+ myCommand.ExecuteNonQuery();
+ myTrans.Commit();
+ Console.WriteLine("Both records are written to database.");
+ }
+ catch(Exception e)
+ {
+ try
+ {
+ myTrans.Rollback();
+ }
+ catch (SqlException ex)
+ {
+ if (myTrans.Connection != null)
+ {
+ Console.WriteLine("An exception of type " + ex.GetType() +
+ " was encountered while attempting to roll back the transaction.");
+ }
+ }
+
+ Console.WriteLine("An exception of type " + e.GetType() +
+ " was encountered while inserting the data.");
+ Console.WriteLine("Neither record was written to database.");
+ }
+ finally
+ {
+ myConnection.Close();
+ }
+ }
+
+
+ Public Sub CreateMySqlConnection()
+ Dim myConnString As String = _
+ "Persist Security Info=False;database=test;server=localhost;user id=joeuser;pwd=pass"
+ Dim myConnection As New MySqlConnection( myConnString )
+ myConnection.Open()
+ MessageBox.Show( "Server Version: " + myConnection.ServerVersion _
+ + ControlChars.NewLine + "Database: " + myConnection.Database )
+ myConnection.ChangeDatabase( "test2" )
+ MessageBox.Show( "ServerVersion: " + myConnection.ServerVersion _
+ + ControlChars.NewLine + "Database: " + myConnection.Database )
+ myConnection.Close()
+ End Sub
+
+
+
+ public void CreateMySqlConnection()
+ {
+ string myConnString =
+ "Persist Security Info=False;database=test;server=localhost;user id=joeuser;pwd=pass";
+ MySqlConnection myConnection = new MySqlConnection( myConnString );
+ myConnection.Open();
+ MessageBox.Show( "Server Version: " + myConnection.ServerVersion
+ + "\nDatabase: " + myConnection.Database );
+ myConnection.ChangeDatabase( "test2" );
+ MessageBox.Show( "ServerVersion: " + myConnection.ServerVersion
+ + "\nDatabase: " + myConnection.Database );
+ myConnection.Close();
+ }
+
+
+ Public Sub CreateMySqlConnection(myConnString As String)
+ Dim myConnection As New MySqlConnection(myConnString)
+ myConnection.Open()
+ MessageBox.Show("ServerVersion: " + myConnection.ServerVersion _
+ + ControlChars.Cr + "State: " + myConnection.State.ToString())
+ myConnection.Close()
+ End Sub
+
+
+ public void CreateMySqlConnection(string myConnString)
+ {
+ MySqlConnection myConnection = new MySqlConnection(myConnString);
+ myConnection.Open();
+ MessageBox.Show("ServerVersion: " + myConnection.ServerVersion +
+ "\nState: " + myConnection.State.ToString());
+ myConnection.Close();
+ }
+
+
+ Public Sub CreateMySqlConnection(myConnString As String)
+ Dim myConnection As New MySqlConnection(myConnString)
+ myConnection.Open()
+ MessageBox.Show("ServerVersion: " + myConnection.ServerVersion _
+ + ControlChars.Cr + "State: " + myConnection.State.ToString())
+ myConnection.Close()
+ End Sub
+
+
+ public void CreateMySqlConnection(string myConnString)
+ {
+ MySqlConnection myConnection = new MySqlConnection(myConnString);
+ myConnection.Open();
+ MessageBox.Show("ServerVersion: " + myConnection.ServerVersion +
+ "\nState: " + myConnection.State.ToString());
+ myConnection.Close();
+ }
+
+
+ Public Sub ReadMyData(myConnString As String)
+ Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
+ Dim myConnection As New MySqlConnection(myConnString)
+ Dim myCommand As New MySqlCommand(mySelectQuery, myConnection)
+ myConnection.Open()
+ Dim myReader As MySqlDataReader
+ myReader = myCommand.ExecuteReader()
+ ' Always call Read before accessing data.
+ While myReader.Read()
+ Console.WriteLine((myReader.GetInt32(0) & ", " & myReader.GetString(1)))
+ End While
+ ' always call Close when done reading.
+ myReader.Close()
+ ' Close the connection when done with it.
+ myConnection.Close()
+ End Sub 'ReadMyData
+
+
+ public void ReadMyData(string myConnString) {
+ string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
+ MySqlConnection myConnection = new MySqlConnection(myConnString);
+ MySqlCommand myCommand = new MySqlCommand(mySelectQuery,myConnection);
+ myConnection.Open();
+ MySqlDataReader myReader;
+ myReader = myCommand.ExecuteReader();
+ // Always call Read before accessing data.
+ while (myReader.Read()) {
+ Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
+ }
+ // always call Close when done reading.
+ myReader.Close();
+ // Close the connection when done with it.
+ myConnection.Close();
+ }
+
+
+ Public Sub ShowException()
+ Dim mySelectQuery As String = "SELECT column1 FROM table1"
+ Dim myConnection As New MySqlConnection ("Data Source=localhost;Database=Sample;")
+ Dim myCommand As New MySqlCommand(mySelectQuery, myConnection)
+
+ Try
+ myCommand.Connection.Open()
+ Catch e As MySqlException
+ MessageBox.Show( e.Message )
+ End Try
+ End Sub
+
+
+ public void ShowException()
+ {
+ string mySelectQuery = "SELECT column1 FROM table1";
+ MySqlConnection myConnection =
+ new MySqlConnection("Data Source=localhost;Database=Sample;");
+ MySqlCommand myCommand = new MySqlCommand(mySelectQuery,myConnection);
+
+ try
+ {
+ myCommand.Connection.Open();
+ }
+ catch (MySqlException e)
+ {
+ MessageBox.Show( e.Message );
+ }
+ }
+
+
+ Public Shared Function SelectRows(myConnection As String, mySelectQuery As String, myTableName As String) As DataSet
+ Dim myConn As New MySqlConnection(myConnection)
+ Dim myDataAdapter As New MySqlDataAdapter()
+ myDataAdapter.SelectCommand = New MySqlCommand(mySelectQuery, myConn)
+ Dim cb As SqlCommandBuilder = New MySqlCommandBuilder(myDataAdapter)
+
+ myConn.Open()
+
+ Dim ds As DataSet = New DataSet
+ myDataAdapter.Fill(ds, myTableName)
+
+ ' Code to modify data in DataSet here
+
+ ' Without the MySqlCommandBuilder this line would fail.
+ myDataAdapter.Update(ds, myTableName)
+
+ myConn.Close()
+ End Function 'SelectRows
+
+
+ public static DataSet SelectRows(string myConnection, string mySelectQuery, string myTableName)
+ {
+ MySqlConnection myConn = new MySqlConnection(myConnection);
+ MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
+ myDataAdapter.SelectCommand = new MySqlCommand(mySelectQuery, myConn);
+ MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
+
+ myConn.Open();
+
+ DataSet ds = new DataSet();
+ myDataAdapter.Fill(ds, myTableName);
+
+ //code to modify data in DataSet here
+
+ //Without the MySqlCommandBuilder this line would fail
+ myDataAdapter.Update(ds, myTableName);
+
+ myConn.Close();
+
+ return ds;
+ }
+
+
+
+ Public Function SelectRows(dataSet As DataSet, connection As String, query As String) As DataSet
+ Dim conn As New MySqlConnection(connection)
+ Dim adapter As New MySqlDataAdapter()
+ adapter.SelectCommand = new MySqlCommand(query, conn)
+ adapter.Fill(dataset)
+ Return dataset
+ End Function
+
+
+ public DataSet SelectRows(DataSet dataset,string connection,string query)
+ {
+ MySqlConnection conn = new MySqlConnection(connection);
+ MySqlDataAdapter adapter = new MySqlDataAdapter();
+ adapter.SelectCommand = new MySqlCommand(query, conn);
+ adapter.Fill(dataset);
+ return dataset;
+ }
+
+
+ Public Sub CreateSqlDataAdapter()
+ Dim conn As MySqlConnection = New MySqlConnection("Data Source=localhost;" & _
+ "database=test")
+ Dim da As MySqlDataAdapter = New MySqlDataAdapter
+ da.MissingSchemaAction = MissingSchemaAction.AddWithKey
+
+ da.SelectCommand = New MySqlCommand("SELECT id, name FROM mytable", conn)
+ da.InsertCommand = New MySqlCommand("INSERT INTO mytable (id, name) " & _
+ "VALUES (@id, @name)", conn)
+ da.UpdateCommand = New MySqlCommand("UPDATE mytable SET id=@id, name=@name " & _
+ "WHERE id=@oldId", conn)
+ da.DeleteCommand = New MySqlCommand("DELETE FROM mytable WHERE id=@id", conn)
+
+ da.InsertCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id")
+ da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name")
+
+ da.UpdateCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id")
+ da.UpdateCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name")
+ da.UpdateCommand.Parameters.Add("@oldId", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original
+
+ da.DeleteCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original
+ End Sub
+
+
+ public static void CreateSqlDataAdapter()
+ {
+ MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=test");
+ MySqlDataAdapter da = new MySqlDataAdapter();
+ da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
+
+ da.SelectCommand = new MySqlCommand("SELECT id, name FROM mytable", conn);
+ da.InsertCommand = new MySqlCommand("INSERT INTO mytable (id, name) " +
+ "VALUES (@id, @name)", conn);
+ da.UpdateCommand = new MySqlCommand("UPDATE mytable SET id=@id, name=@name " +
+ "WHERE id=@oldId", conn);
+ da.DeleteCommand = new MySqlCommand("DELETE FROM mytable WHERE id=@id", conn);
+
+ da.InsertCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id");
+ da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name");
+
+ da.UpdateCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id");
+ da.UpdateCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name");
+ da.UpdateCommand.Parameters.Add("@oldId", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original;
+
+ da.DeleteCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original;
+ }
+
+
+ Public Sub CreateSqlDataAdapter()
+ Dim conn As MySqlConnection = New MySqlConnection("Data Source=localhost;" & _
+ "database=test")
+ Dim cmd as new MySqlCommand("SELECT id, name FROM mytable", conn)
+ Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
+ da.MissingSchemaAction = MissingSchemaAction.AddWithKey
+
+ da.InsertCommand = New MySqlCommand("INSERT INTO mytable (id, name) " & _
+ "VALUES (@id, @name)", conn)
+ da.UpdateCommand = New MySqlCommand("UPDATE mytable SET id=@id, name=@name " & _
+ "WHERE id=@oldId", conn)
+ da.DeleteCommand = New MySqlCommand("DELETE FROM mytable WHERE id=@id", conn)
+
+ da.InsertCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id")
+ da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name")
+
+ da.UpdateCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id")
+ da.UpdateCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name")
+ da.UpdateCommand.Parameters.Add("@oldId", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original
+
+ da.DeleteCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original
+ End Sub
+
+
+ public static void CreateSqlDataAdapter()
+ {
+ MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=test");
+ MySqlCommand cmd = new MySqlCommand("SELECT id, name FROM mytable", conn);
+ MySqlDataAdapter da = new MySqlDataAdapter(cmd);
+ da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
+
+ da.InsertCommand = new MySqlCommand("INSERT INTO mytable (id, name) " +
+ "VALUES (@id, @name)", conn);
+ da.UpdateCommand = new MySqlCommand("UPDATE mytable SET id=@id, name=@name " +
+ "WHERE id=@oldId", conn);
+ da.DeleteCommand = new MySqlCommand("DELETE FROM mytable WHERE id=@id", conn);
+
+ da.InsertCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id");
+ da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name");
+
+ da.UpdateCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id");
+ da.UpdateCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name");
+ da.UpdateCommand.Parameters.Add("@oldId", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original;
+
+ da.DeleteCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original;
+ }
+
+
+ Public Sub CreateSqlDataAdapter()
+ Dim conn As MySqlConnection = New MySqlConnection("Data Source=localhost;" & _
+ "database=test")
+ Dim da As MySqlDataAdapter = New MySqlDataAdapter("SELECT id, name FROM mytable", conn)
+ da.MissingSchemaAction = MissingSchemaAction.AddWithKey
+
+ da.InsertCommand = New MySqlCommand("INSERT INTO mytable (id, name) " & _
+ "VALUES (@id, @name)", conn)
+ da.UpdateCommand = New MySqlCommand("UPDATE mytable SET id=@id, name=@name " & _
+ "WHERE id=@oldId", conn)
+ da.DeleteCommand = New MySqlCommand("DELETE FROM mytable WHERE id=@id", conn)
+
+ da.InsertCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id")
+ da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name")
+
+ da.UpdateCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id")
+ da.UpdateCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name")
+ da.UpdateCommand.Parameters.Add("@oldId", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original
+
+ da.DeleteCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original
+ End Sub
+
+
+ public static void CreateSqlDataAdapter()
+ {
+ MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=test");
+ MySqlDataAdapter da = new MySqlDataAdapter("SELECT id, name FROM mytable", conn);
+ da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
+
+ da.InsertCommand = new MySqlCommand("INSERT INTO mytable (id, name) " +
+ "VALUES (@id, @name)", conn);
+ da.UpdateCommand = new MySqlCommand("UPDATE mytable SET id=@id, name=@name " +
+ "WHERE id=@oldId", conn);
+ da.DeleteCommand = new MySqlCommand("DELETE FROM mytable WHERE id=@id", conn);
+
+ da.InsertCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id");
+ da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name");
+
+ da.UpdateCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id");
+ da.UpdateCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name");
+ da.UpdateCommand.Parameters.Add("@oldId", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original;
+
+ da.DeleteCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original;
+ }
+
+
+ Public Sub CreateSqlDataAdapter()
+ Dim da As MySqlDataAdapter = New MySqlDataAdapter("SELECT id, name FROM mytable", "Data Source=localhost;database=test")
+ Dim conn As MySqlConnection = da.SelectCommand.Connection
+ da.MissingSchemaAction = MissingSchemaAction.AddWithKey
+
+ da.InsertCommand = New MySqlCommand("INSERT INTO mytable (id, name) " & _
+ "VALUES (@id, @name)", conn)
+ da.UpdateCommand = New MySqlCommand("UPDATE mytable SET id=@id, name=@name " & _
+ "WHERE id=@oldId", conn)
+ da.DeleteCommand = New MySqlCommand("DELETE FROM mytable WHERE id=@id", conn)
+
+ da.InsertCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id")
+ da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name")
+
+ da.UpdateCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id")
+ da.UpdateCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name")
+ da.UpdateCommand.Parameters.Add("@oldId", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original
+
+ da.DeleteCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original
+ End Sub
+
+
+ public static void CreateSqlDataAdapter()
+ {
+ MySqlDataAdapter da = new MySqlDataAdapter("SELECT id, name FROM mytable", "Data Source=localhost;database=test");
+ MySqlConnection conn = da.SelectCommand.Connection;
+ da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
+
+ da.InsertCommand = new MySqlCommand("INSERT INTO mytable (id, name) " +
+ "VALUES (@id, @name)", conn);
+ da.UpdateCommand = new MySqlCommand("UPDATE mytable SET id=@id, name=@name " +
+ "WHERE id=@oldId", conn);
+ da.DeleteCommand = new MySqlCommand("DELETE FROM mytable WHERE id=@id", conn);
+
+ da.InsertCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id");
+ da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name");
+
+ da.UpdateCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id");
+ da.UpdateCommand.Parameters.Add("@name", MySqlDbType.VarChar, 40, "name");
+ da.UpdateCommand.Parameters.Add("@oldId", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original;
+
+ da.DeleteCommand.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original;
+ }
+
+
+ Public Shared Function CreateCustomerAdapter(conn As MySqlConnection) As MySqlDataAdapter
+ Dim da As MySqlDataAdapter = New MySqlDataAdapter()
+ Dim cmd As MySqlCommand
+ Dim parm As MySqlParameter
+
+ ' Create the SelectCommand.
+ cmd = New MySqlCommand("SELECT * FROM mytable WHERE id=@id AND name=@name", conn)
+
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15)
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15)
+
+ da.SelectCommand = cmd
+
+ ' Create the DeleteCommand.
+ cmd = New MySqlCommand("DELETE FROM mytable WHERE id=@id", conn)
+
+ parm = cmd.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id")
+ parm.SourceVersion = DataRowVersion.Original
+
+ da.DeleteCommand = cmd
+
+ Return da
+ End Function
+
+
+ public static MySqlDataAdapter CreateCustomerAdapter(MySqlConnection conn)
+ {
+ MySqlDataAdapter da = new MySqlDataAdapter();
+ MySqlCommand cmd;
+ MySqlParameter parm;
+
+ // Create the SelectCommand.
+ cmd = new MySqlCommand("SELECT * FROM mytable WHERE id=@id AND name=@name", conn);
+
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15);
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15);
+
+ da.SelectCommand = cmd;
+
+ // Create the DeleteCommand.
+ cmd = new MySqlCommand("DELETE FROM mytable WHERE id=@id", conn);
+
+ parm = cmd.Parameters.Add("@id", MySqlDbType.VarChar, 5, "id");
+ parm.SourceVersion = DataRowVersion.Original;
+
+ da.DeleteCommand = cmd;
+
+ return da;
+ }
+
+
+ Public Shared Function CreateCustomerAdapter(conn As MySqlConnection) As MySqlDataAdapter
+ Dim da As MySqlDataAdapter = New MySqlDataAdapter()
+ Dim cmd As MySqlCommand
+ Dim parm As MySqlParameter
+
+ ' Create the SelectCommand.
+ cmd = New MySqlCommand("SELECT * FROM mytable WHERE id=@id AND name=@name", conn)
+
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15)
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15)
+
+ da.SelectCommand = cmd
+
+ ' Create the InsertCommand.
+ cmd = New MySqlCommand("INSERT INTO mytable (id,name) VALUES (@id, @name)", conn)
+
+ cmd.Parameters.Add( "@id", MySqlDbType.VarChar, 15, "id" )
+ cmd.Parameters.Add( "@name", MySqlDbType.VarChar, 15, "name" )
+ da.InsertCommand = cmd
+
+ Return da
+ End Function
+
+
+ public static MySqlDataAdapter CreateCustomerAdapter(MySqlConnection conn)
+ {
+ MySqlDataAdapter da = new MySqlDataAdapter();
+ MySqlCommand cmd;
+ MySqlParameter parm;
+
+ // Create the SelectCommand.
+ cmd = new MySqlCommand("SELECT * FROM mytable WHERE id=@id AND name=@name", conn);
+
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15);
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15);
+
+ da.SelectCommand = cmd;
+
+ // Create the InsertCommand.
+ cmd = new MySqlCommand("INSERT INTO mytable (id,name) VALUES (@id,@name)", conn);
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15, "id" );
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15, "name" );
+
+ da.InsertCommand = cmd;
+
+ return da;
+ }
+
+
+ Public Shared Function CreateCustomerAdapter(conn As MySqlConnection) As MySqlDataAdapter
+ Dim da As MySqlDataAdapter = New MySqlDataAdapter()
+ Dim cmd As MySqlCommand
+ Dim parm As MySqlParameter
+
+ ' Create the SelectCommand.
+ cmd = New MySqlCommand("SELECT * FROM mytable WHERE id=@id AND name=@name", conn)
+
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15)
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15)
+
+ da.SelectCommand = cmd
+
+ ' Create the InsertCommand.
+ cmd = New MySqlCommand("INSERT INTO mytable (id,name) VALUES (@id, @name)", conn)
+
+ cmd.Parameters.Add( "@id", MySqlDbType.VarChar, 15, "id" )
+ cmd.Parameters.Add( "@name", MySqlDbType.VarChar, 15, "name" )
+ da.InsertCommand = cmd
+
+ Return da
+ End Function
+
+
+ public static MySqlDataAdapter CreateCustomerAdapter(MySqlConnection conn)
+ {
+ MySqlDataAdapter da = new MySqlDataAdapter();
+ MySqlCommand cmd;
+ MySqlParameter parm;
+
+ // Create the SelectCommand.
+ cmd = new MySqlCommand("SELECT * FROM mytable WHERE id=@id AND name=@name", conn);
+
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15);
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15);
+
+ da.SelectCommand = cmd;
+
+ // Create the InsertCommand.
+ cmd = new MySqlCommand("INSERT INTO mytable (id,name) VALUES (@id,@name)", conn);
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15, "id" );
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15, "name" );
+
+ da.InsertCommand = cmd;
+
+ return da;
+ }
+
+
+ Public Shared Function CreateCustomerAdapter(conn As MySqlConnection) As MySqlDataAdapter
+ Dim da As MySqlDataAdapter = New MySqlDataAdapter()
+ Dim cmd As MySqlCommand
+ Dim parm As MySqlParameter
+
+ ' Create the SelectCommand.
+ cmd = New MySqlCommand("SELECT * FROM mytable WHERE id=@id AND name=@name", conn)
+
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15)
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15)
+
+ da.SelectCommand = cmd
+
+ ' Create the UpdateCommand.
+ cmd = New MySqlCommand("UPDATE mytable SET id=@id, name=@name WHERE id=@oldId", conn)
+
+ cmd.Parameters.Add( "@id", MySqlDbType.VarChar, 15, "id" )
+ cmd.Parameters.Add( "@name", MySqlDbType.VarChar, 15, "name" )
+
+ parm = cmd.Parameters.Add("@oldId", MySqlDbType.VarChar, 15, "id")
+ parm.SourceVersion = DataRowVersion.Original
+
+ da.UpdateCommand = cmd
+
+ Return da
+ End Function
+
+
+ public static MySqlDataAdapter CreateCustomerAdapter(MySqlConnection conn)
+ {
+ MySqlDataAdapter da = new MySqlDataAdapter();
+ MySqlCommand cmd;
+ MySqlParameter parm;
+
+ // Create the SelectCommand.
+ cmd = new MySqlCommand("SELECT * FROM mytable WHERE id=@id AND name=@name", conn);
+
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15);
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15);
+
+ da.SelectCommand = cmd;
+
+ // Create the UpdateCommand.
+ cmd = new MySqlCommand("UPDATE mytable SET id=@id, name=@name WHERE id=@oldId", conn);
+ cmd.Parameters.Add("@id", MySqlDbType.VarChar, 15, "id" );
+ cmd.Parameters.Add("@name", MySqlDbType.VarChar, 15, "name" );
+
+ parm = cmd.Parameters.Add( "@oldId", MySqlDbType.VarChar, 15, "id" );
+ parm.SourceVersion = DataRowVersion.Original;
+
+ da.UpdateCommand = cmd;
+
+ return da;
+ }
+
+
+ Public Sub AddParameters()
+ ' ...
+ ' create myDataSet and myDataAdapter
+ ' ...
+ myDataAdapter.SelectCommand.Parameters.Add("@CategoryName", MySqlDbType.VarChar, 80).Value = "toasters"
+ myDataAdapter.SelectCommand.Parameters.Add("@SerialNum", MySqlDbType.Long).Value = 239
+
+ myDataAdapter.Fill(myDataSet)
+ End Sub 'AddSqlParameters
+
+
+ public void AddSqlParameters()
+ {
+ // ...
+ // create myDataSet and myDataAdapter
+ // ...
+
+ myDataAdapter.SelectCommand.Parameters.Add("@CategoryName", MySqlDbType.VarChar, 80).Value = "toasters";
+ myDataAdapter.SelectCommand.Parameters.Add("@SerialNum", MySqlDbType.Long).Value = 239;
+ myDataAdapter.Fill(myDataSet);
+ }
+
+
+ Public Sub AddQueryAttributes()
+ ' ...
+ ' create MySqlCommand
+ ' ...
+ myCommand.Attributes.SetAttribute("queryAttribute", "value of the query attribute")
+ myCommand.Attributes.SetAttribute("queryAttribute2", DateTime.Now)
+ End Sub
+
+
+ public void AddQueryAttributes()
+ {
+ // ...
+ // create MySqlCommand
+ // ...
+
+ myCommand.Attributes.SetAttribute("queryAttribute", "value of the query attribute");
+ myCommand.Attributes.SetAttribute("queryAttribute2", DateTime.Now);
+ }
+
+
+ Public Sub RunTransaction(myConnString As String)
+ Dim myConnection As New MySqlConnection(myConnString)
+ myConnection.Open()
+
+ Dim myCommand As MySqlCommand = myConnection.CreateCommand()
+ Dim myTrans As MySqlTransaction
+
+ ' Start a local transaction
+ myTrans = myConnection.BeginTransaction()
+ ' Must assign both transaction object and connection
+ ' to Command object for a pending local transaction
+ myCommand.Connection = myConnection
+ myCommand.Transaction = myTrans
+
+ Try
+ myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
+ myCommand.ExecuteNonQuery()
+ myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
+ myCommand.ExecuteNonQuery()
+ myTrans.Commit()
+ Console.WriteLine("Both records are written to database.")
+ Catch e As Exception
+ Try
+ myTrans.Rollback()
+ Catch ex As MySqlException
+ If Not myTrans.Connection Is Nothing Then
+ Console.WriteLine("An exception of type " & ex.GetType().ToString() & _
+ " was encountered while attempting to roll back the transaction.")
+ End If
+ End Try
+
+ Console.WriteLine("An exception of type " & e.GetType().ToString() & _
+ "was encountered while inserting the data.")
+ Console.WriteLine("Neither record was written to database.")
+ Finally
+ myConnection.Close()
+ End Try
+ End Sub 'RunTransaction
+
+
+ public void RunTransaction(string myConnString)
+ {
+ MySqlConnection myConnection = new MySqlConnection(myConnString);
+ myConnection.Open();
+
+ MySqlCommand myCommand = myConnection.CreateCommand();
+ MySqlTransaction myTrans;
+
+ // Start a local transaction
+ myTrans = myConnection.BeginTransaction();
+ // Must assign both transaction object and connection
+ // to Command object for a pending local transaction
+ myCommand.Connection = myConnection;
+ myCommand.Transaction = myTrans;
+
+ try
+ {
+ myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
+ myCommand.ExecuteNonQuery();
+ myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
+ myCommand.ExecuteNonQuery();
+ myTrans.Commit();
+ Console.WriteLine("Both records are written to database.");
+ }
+ catch(Exception e)
+ {
+ try
+ {
+ myTrans.Rollback();
+ }
+ catch (MySqlException ex)
+ {
+ if (myTrans.Connection != null)
+ {
+ Console.WriteLine("An exception of type " + ex.GetType() +
+ " was encountered while attempting to roll back the transaction.");
+ }
+ }
+
+ Console.WriteLine("An exception of type " + e.GetType() +
+ " was encountered while inserting the data.");
+ Console.WriteLine("Neither record was written to database.");
+ }
+ finally
+ {
+ myConnection.Close();
+ }
+ }
+
+
+ Public Sub RunSqlTransaction(myConnString As String)
+ Dim myConnection As New MySqlConnection(myConnString)
+ myConnection.Open()
+
+ Dim myCommand As MySqlCommand = myConnection.CreateCommand()
+ Dim myTrans As MySqlTransaction
+
+ ' Start a local transaction
+ myTrans = myConnection.BeginTransaction()
+
+ ' Must assign both transaction object and connection
+ ' to Command object for a pending local transaction
+ myCommand.Connection = myConnection
+ myCommand.Transaction = myTrans
+
+ Try
+ myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')"
+ myCommand.ExecuteNonQuery()
+ myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')"
+ myCommand.ExecuteNonQuery()
+ myTrans.Commit()
+ Console.WriteLine("Success.")
+ Catch e As Exception
+ Try
+ myTrans.Rollback()
+ Catch ex As MySqlException
+ If Not myTrans.Connection Is Nothing Then
+ Console.WriteLine("An exception of type " & ex.GetType().ToString() & _
+ " was encountered while attempting to roll back the transaction.")
+ End If
+ End Try
+
+ Console.WriteLine("An exception of type " & e.GetType().ToString() & _
+ "was encountered while inserting the data.")
+ Console.WriteLine("Neither record was written to database.")
+ Finally
+ myConnection.Close()
+ End Try
+ End Sub
+
+
+ public void RunSqlTransaction(string myConnString)
+ {
+ MySqlConnection myConnection = new MySqlConnection(myConnString);
+ myConnection.Open();
+
+ MySqlCommand myCommand = myConnection.CreateCommand();
+ MySqlTransaction myTrans;
+
+ // Start a local transaction
+ myTrans = myConnection.BeginTransaction();
+ // Must assign both transaction object and connection
+ // to Command object for a pending local transaction
+ myCommand.Connection = myConnection;
+ myCommand.Transaction = myTrans;
+
+ try
+ {
+ myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')";
+ myCommand.ExecuteNonQuery();
+ myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')";
+ myCommand.ExecuteNonQuery();
+ myTrans.Commit();
+ Console.WriteLine("Both records are written to database.");
+ }
+ catch(Exception e)
+ {
+ try
+ {
+ myTrans.Rollback();
+ }
+ catch (MySqlException ex)
+ {
+ if (myTrans.Connection != null)
+ {
+ Console.WriteLine("An exception of type " + ex.GetType() +
+ " was encountered while attempting to roll back the transaction.");
+ }
+ }
+
+ Console.WriteLine("An exception of type " + e.GetType() +
+ " was encountered while inserting the data.");
+ Console.WriteLine("Neither record was written to database.");
+ }
+ finally
+ {
+ myConnection.Close();
+ }
+ }
+
+
+ Public Sub RunSqlTransaction(myConnString As String)
+ Dim myConnection As New MySqlConnection(myConnString)
+ myConnection.Open()
+
+ Dim myCommand As MySqlCommand = myConnection.CreateCommand()
+ Dim myTrans As MySqlTransaction
+
+ ' Start a local transaction
+ myTrans = myConnection.BeginTransaction()
+
+ ' Must assign both transaction object and connection
+ ' to Command object for a pending local transaction
+ myCommand.Connection = myConnection
+ myCommand.Transaction = myTrans
+
+ Try
+ myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')"
+ myCommand.ExecuteNonQuery()
+ myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')"
+ myCommand.ExecuteNonQuery()
+ myTrans.Commit()
+ Console.WriteLine("Success.")
+ Catch e As Exception
+ Try
+ myTrans.Rollback()
+ Catch ex As MySqlException
+ If Not myTrans.Connection Is Nothing Then
+ Console.WriteLine("An exception of type " & ex.GetType().ToString() & _
+ " was encountered while attempting to roll back the transaction.")
+ End If
+ End Try
+
+ Console.WriteLine("An exception of type " & e.GetType().ToString() & _
+ "was encountered while inserting the data.")
+ Console.WriteLine("Neither record was written to database.")
+ Finally
+ myConnection.Close()
+ End Try
+ End Sub
+
+
+ public void RunSqlTransaction(string myConnString)
+ {
+ MySqlConnection myConnection = new MySqlConnection(myConnString);
+ myConnection.Open();
+
+ MySqlCommand myCommand = myConnection.CreateCommand();
+ MySqlTransaction myTrans;
+
+ // Start a local transaction
+ myTrans = myConnection.BeginTransaction();
+ // Must assign both transaction object and connection
+ // to Command object for a pending local transaction
+ myCommand.Connection = myConnection;
+ myCommand.Transaction = myTrans;
+
+ try
+ {
+ myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')";
+ myCommand.ExecuteNonQuery();
+ myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')";
+ myCommand.ExecuteNonQuery();
+ myTrans.Commit();
+ Console.WriteLine("Both records are written to database.");
+ }
+ catch(Exception e)
+ {
+ try
+ {
+ myTrans.Rollback();
+ }
+ catch (MySqlException ex)
+ {
+ if (myTrans.Connection != null)
+ {
+ Console.WriteLine("An exception of type " + ex.GetType() +
+ " was encountered while attempting to roll back the transaction.");
+ }
+ }
+
+ Console.WriteLine("An exception of type " + e.GetType() +
+ " was encountered while inserting the data.");
+ Console.WriteLine("Neither record was written to database.");
+ }
+ finally
+ {
+ myConnection.Close();
+ }
+ }
+
+ ref
+ ref
+
+ new { pooling = new
+ {
+ enabled = true,
+ maxSize = 15,
+ maxIdleTime = 60000,
+ queueTimeout = 60000
+ }
+ }
+
+
+ new { pooling = new
+ {
+ enabled = true,
+ maxSize = 15,
+ maxIdleTime = 60000,
+ queueTimeout = 60000
+ }
+ }
+
+