删除组合框项c#

时间:2014-10-19 05:17:09

标签: c# winforms combobox

我在检查数据库中的“Administrator”文本后,尝试删除组合框项目列表中的“Administrator”文本,如果找到,则会只需从下拉列表菜单中删除即可。

现在,即使数据库中有“Administrator”文本,组合框仍然会将“Administrator”文本添加到项目列表中。< / p>

某处错了吗?

以下是我正在使用的代码:

public partial class Registration: Form
{

bool _isCheckedEmail = false;

void Registration_Load(object sender, EventArgs e)
        {
            ClearTextBoxes();

            SystemManager.CheckType("Administrator");

            if (SystemManager._isUsed != true)
            {
                this.comboBox1.Items.Add("Administrator");
            }

            this.comboBox1.Items.Add("Member");

            this.comboBox1.SelectedIndex = 0;

            SystemManager._isCheckedDisplayName = false;
            _isCheckedEmail = false;

            this.button1.Enabled = false;
            this.button4.Enabled = false;
        }
}

这是 “SystemManager” 类:

public class SystemManager
{

public static bool _isUsed = false;

public static void CheckType(string _value1)
        {
            using (OleDbConnection connection = new OleDbConnection(SystemManager.connectionString))
            {
                string query = "SELECT COUNT(*) FROM [Member] WHERE [UserType] = @UserType";

                connection.Open();

                using (OleDbCommand command = new OleDbCommand(query, connection))
                {
                    command.Parameters.Add("@UserType", OleDbType.VarChar);
                    command.Parameters["@UserType"].Value = _value1;

                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (_count > 0)
                            {
                                _isUsed = true;
                            }

                            else
                            {
                                _isUsed = false;
                            }
                        }

                        reader.Close();
                    }
                }

                connection.Close();
            }

        }
}

这是数据库(我将其上传到保管箱):

Link

谢谢。

2 个答案:

答案 0 :(得分:1)

1

您可以通过reader[0]引用结果对象。

using (OleDbDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        Integer.TryParse(reader[0].ToString(), _count);
        if (_count > 0)
        {
            _isUsed = true;
        }

        else
        {
            _isUsed = false;
        }
    }
    reader.Close();
}

2

使用OleDbDataReader.HasRows属性检查类型是否存在。

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader.hasrows(v=vs.110).aspx

string query = "SELECT * FROM [Member] WHERE [UserType] = @UserType";

connection.Open();

using (OleDbCommand command = new OleDbCommand(query, connection))
{
    command.Parameters.Add("@UserType", OleDbType.VarChar);
    command.Parameters["@UserType"].Value = _value1;

    using (OleDbDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            if (reader.HasRows)
            {
                _isUsed = true;
            }

            else
            {
                _isUsed = false;
            }
    }
    reader.Close();
}

答案 1 :(得分:0)

以上是上述问题的解决方案:

    void Registration_Load(object sender, EventArgs e)
    {
        SystemManager.ClearTextBoxes(this.Controls);

        if (SystemManager.CheckType("Administrator") != true)
        {
            this.comboBox1.Items.Add("Administrator");
        }

        this.comboBox1.Items.Add("Member");

        this.comboBox1.SelectedIndex = 0;

        SystemManager._isCheckedDisplayName = false;
        _isCheckedEmail = false;

        this.button1.Enabled = false;
        this.button4.Enabled = false;
    }

       public static bool CheckType(string _value1)
        {
            using (OleDbConnection connection = new OleDbConnection(SystemManager.connectionString))
            {
                string query = "SELECT COUNT(*) FROM [Member] WHERE [UserType] = @UserType";

                connection.Open();

                using (OleDbCommand command = new OleDbCommand(query, connection))
                {
                    command.Parameters.Add("@UserType", OleDbType.VarChar);
                    command.Parameters["@UserType"].Value = _value1;

                    _count = (int)command.ExecuteScalar();

                    connection.Close();
                }

            }

            return _count > 0;
        }