示例#1
0
        /// <summary>
        /// 添加变量
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void bAdd_Click(object sender, EventArgs e)
        {
            AddForm      addForm = new AddForm(conn);
            DialogResult result  = addForm.ShowDialog();

            if (result == DialogResult.OK)
            {
                Hashtable newVarData   = addForm.NewVarData;
                string    name         = newVarData["name"] as string;
                string    userName     = newVarData["userName"] as string;
                string    varType      = newVarData["type"] as string;
                string    desctiption  = newVarData["description"] as string;
                string    bindType     = newVarData["bindType"] as string;
                string    bindObject   = newVarData["bindObject"] as string;
                string    bindID       = newVarData["bindID"] as string;
                string    stringLength = newVarData["stringLength"] as string;


                // 锁定绑定的对象
                bool lockSuccess = LockVarData(bindType, bindID, userName);
                if (lockSuccess) // 锁定成功
                {
                    string sqlString = "SELECT * FROM VarData ORDER BY StartIndex";
                    try
                    {
                        if (conn.State == ConnectionState.Closed)
                        {
                            conn.Open();
                        }

                        // 刷新数据表里的数据
                        table = GetDataTable(sqlString);
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show("在读取变量信息时产生sql异常:" + ex.ToString());
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)
                        {
                            conn.Close();
                        }
                    }

                    // 计算出新加变量的偏移和长度
                    int varLength = 0;
                    switch (varType)
                    {
                    case "布尔":
                    {
                        varLength = 1;
                        break;
                    }

                    case "8位无符号整型":
                    {
                        varLength = 1;
                        break;
                    }

                    case "16位无符号整型":
                    {
                        varLength = 2;
                        break;
                    }

                    case "32位无符号整型":
                    {
                        varLength = 4;
                        break;
                    }

                    case "8位有符号整型":
                    {
                        varLength = 1;
                        break;
                    }

                    case "16位有符号整型":
                    {
                        varLength = 2;
                        break;
                    }

                    case "32位有符号整型":
                    {
                        varLength = 4;
                        break;
                    }

                    case "字符串":
                    {
                        varLength = int.Parse(stringLength) * 2;
                        break;
                    }
                    }

                    int varOffset = GetVarOffset(varLength, bindType, bindID);
                    if (varOffset >= 0) // 找到可分配的空间
                    {
                        // 更新数据库
                        try
                        {
                            if (conn.State == ConnectionState.Closed)
                            {
                                conn.Open();
                            }

                            sqlString = string.Format("INSERT INTO VarData (Name, UserName, Type, Description, StartIndex, Length, Version, BindID, BindType, BindObject) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}')",
                                                      name, userName, varType, desctiption, varOffset, varLength, 1, bindID, bindType, bindObject);
                            SqlCommand cmd = conn.CreateCommand();
                            cmd.CommandText = sqlString;
                            cmd.ExecuteNonQuery();
                            sqlString = "SELECT * FROM VarData";
                            table     = GetDataTable(sqlString);
                        }
                        catch (SqlException ex)
                        {
                            MessageBox.Show("在更新变量信息时产生sql异常:" + ex.ToString());
                        }
                        finally
                        {
                            if (conn.State == ConnectionState.Open)
                            {
                                conn.Close();
                            }
                        }

                        // 解除绑定
                        UnlockVarData(bindType, bindID, userName);

                        // 刷新数据
                        RefreshDataGridView("所有用户", "所有变量类型", "所有绑定类型", "所有绑定对象");
                    }
                    else
                    {
                        MessageBox.Show("自定义数据块已满,无法分配空间给当前变量!", "变量管理",
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("当前绑定对象已经被锁定,请等待解锁后再申请新建变量!", "变量管理",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
示例#2
0
文件: MainForm.cs 项目: viticm/pap2
        /// <summary>
        /// 添加变量
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void bAdd_Click(object sender, EventArgs e)
        {
            AddForm addForm = new AddForm(conn);
            DialogResult result = addForm.ShowDialog();
            if(result == DialogResult.OK)
            {
                Hashtable newVarData = addForm.NewVarData;
                string name = newVarData["name"] as string;
                string userName = newVarData["userName"] as string;
                string varType = newVarData["type"] as string;
                string desctiption = newVarData["description"] as string;
                string bindType = newVarData["bindType"] as string;
                string bindObject = newVarData["bindObject"] as string;
                string bindID = newVarData["bindID"] as string;
                string stringLength = newVarData["stringLength"] as string;

                
                // 锁定绑定的对象
                bool lockSuccess = LockVarData(bindType, bindID, userName);
                if(lockSuccess) // 锁定成功
                {
                    string sqlString = "SELECT * FROM VarData ORDER BY StartIndex";
                    try
                    {
                        if (conn.State == ConnectionState.Closed)
                        {
                            conn.Open();
                        }

                        // 刷新数据表里的数据
                        table = GetDataTable(sqlString);
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show("在读取变量信息时产生sql异常:" + ex.ToString());
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)
                        {
                            conn.Close();
                        }
                    }

                    // 计算出新加变量的偏移和长度
                    int varLength = 0;
                    switch (varType)
                    {
                        case "布尔":
                            {
                                varLength = 1;
                                break;
                            }
                        case "8位无符号整型":
                            {
                                varLength = 1;
                                break;
                            }
                        case "16位无符号整型":
                            {
                                varLength = 2;
                                break;
                            }
                        case "32位无符号整型":
                            {
                                varLength = 4;
                                break;
                            }
                        case "8位有符号整型":
                            {
                                varLength = 1;
                                break;
                            }
                        case "16位有符号整型":
                            {
                                varLength = 2;
                                break;
                            }
                        case "32位有符号整型":
                            {
                                varLength = 4;
                                break;
                            }
                        case "字符串":
                            {
                                varLength = int.Parse(stringLength) * 2;
                                break;
                            }
                    }

                    int varOffset = GetVarOffset(varLength, bindType, bindID);
                    if (varOffset >= 0) // 找到可分配的空间
                    {
                        // 更新数据库
                        try
                        {
                            if (conn.State == ConnectionState.Closed)
                            {
                                conn.Open();
                            }

                            sqlString = string.Format("INSERT INTO VarData (Name, UserName, Type, Description, StartIndex, Length, Version, BindID, BindType, BindObject) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}')",
                                name, userName, varType, desctiption, varOffset, varLength, 1, bindID, bindType, bindObject);
                            SqlCommand cmd = conn.CreateCommand();
                            cmd.CommandText = sqlString;
                            cmd.ExecuteNonQuery();
                            sqlString = "SELECT * FROM VarData";
                            table = GetDataTable(sqlString);
                        }
                        catch (SqlException ex)
                        {
                            MessageBox.Show("在更新变量信息时产生sql异常:" + ex.ToString());
                        }
                        finally
                        {
                            if (conn.State == ConnectionState.Open)
                            {
                                conn.Close();
                            }
                        }

                        // 解除绑定
                        UnlockVarData(bindType, bindID, userName);

                        // 刷新数据
                        RefreshDataGridView("所有用户", "所有变量类型", "所有绑定类型", "所有绑定对象");
                    }
                    else
                    {
                        MessageBox.Show("自定义数据块已满,无法分配空间给当前变量!", "变量管理",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("当前绑定对象已经被锁定,请等待解锁后再申请新建变量!", "变量管理",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }                
            }
        }