删除组合框项目c#

本文关键字:项目 组合 删除 | 更新日期: 2023-09-27 18:26:58

在检查了数据库中的"Administrator"文本后,我正试图删除组合框项目列表中的"管理员"

现在,即使数据库中有"管理员"文本,组合框仍会将的"管理员"文本添加到其项目列表中。

我哪里错了?

这是我正在使用的代码:

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();
            }
        }
}

这是数据库(我正在上传到投递箱):

链接

谢谢。

删除组合框项目c#

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();
}

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

    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;
        }