Question : Locking MySql Table from C#;

I am attempting to lock a mysql table from c# and have successfully done so, with one problem.  I thought that ideally the table that I lock, would stay locked until I explicitly give the "Unlock Tables" command.  The reason I ask is that I have to execute a select query and an update query on a table before allowing anyone else to access it.  It appears that within my application(2 instances running), I can lock a table from the first instance, attempt to read a value from that table from the second instance(which hangs), and the second I read a value from the first instance the second instance unhangs and completes the execution.  It seems that the table lock is released the moment a command is given to that table from the locking instance.  Any insight on this?????????????????
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
//G1.conn1 returns a new instance of a connection to the database.		
 
                private void btnLock_Click(object sender, System.EventArgs e)
		{
			MySqlCommand cmd = new MySqlCommand("Lock Table TestTable write, TestTable as t1 write", G1.conn1);
			G1.TryNonQueryStatic(cmd);
		}
 
		private void btnUnlock_Click(object sender, System.EventArgs e)
		{
			MySqlCommand cmd = new MySqlCommand("Unlock Tables",G1.conn1);
			G1.TryNonQueryStatic(cmd);
		}
 
		private void btnWrite_Click(object sender, System.EventArgs e)
		{
			Double dd;
			if((txtWrite.Text.Length > 0)&&(Double.TryParse(txtWrite.Text,System.Globalization.NumberStyles.Number,null,out dd)))
			{
				MySqlCommand cmd = new MySqlCommand("Update testtable as t1 set testdouble = " + Double.Parse(txtWrite.Text).ToString(),G1.conn1);
				int affected = G1.TryNonQueryStatic(cmd);
				MessageBox.Show("Affected = " + affected.ToString());
			}
			else
				MessageBox.Show("Textbox value is invalid");
		}
 
		private void btnRead_Click(object sender, System.EventArgs e)
		{
			MySqlCommand cmd = new MySqlCommand("Select testdouble from testtable",G1.conn1);
			lblRead.Text = G1.TryScalarStatic(cmd);
		}

Answer : Locking MySql Table from C#;

but you are closing the connection, which releases the locks also.
you have to do all the work on the same connection, without closing it.
Random Solutions  
 
programming4us programming4us