我试图在访问单个 MySQL (InnoDB) 数据库的多个自动化服务之间保持数据一致性。目前,我正在尝试使用 GET_LOCK 来完成此操作。我正在使用自动生成的类型化数据集,并配置了以下查询:
SELECT GET_LOCK('@id', 1)
适配器也接受一个Int32
作为参数。问题是该'@id'
字段被解释为字符串文字,因此我无法获得动态分配的锁定字符串。我的问题是这是否可能。
我目前的解决方法是:
DataSetTableAdapters.myTableAdapter typedAdapter = new DataSetTableAdapters.myTableAdapter();
MySql.Data.MySqlClient.MySqlCommand selectCommand_backup = typedAdapter.Adapter.SelectCommand;
typedAdapter.Adapter.SelectCommand = new MySql.Data.MySqlClient.MySqlCommand();
typedAdapter.Adapter.SelectCommand.Connection = typedAdapter.Connection;
typedAdapter.Adapter.SelectCommand.CommandText = string.Format("SELECT GET_LOCK('{0}_{1}', 1)", tableName, id);
typedAdapter.Adapter.SelectCommand.CommandType = System.Data.CommandType.Text;
typedAdapter.Connection.Open();
typedAdapter.Adapter.SelectCommand.ExecuteScalar();
typedAdapter.Connection.Close();
typedAdapter.Adapter.SelectCommand = selectCommand_backup;
我也对其他想法持开放态度。如果可能,我想防止冲突解决,因为处理自动化系统将是一项挑战。