WPF - 由于出现连接异常,我无法启动应用

WPF - I cannot start the app because of the connection exception that i'm getting

提问人:Aleksandar Veljkovic 提问时间:2/18/2023 最后编辑:marc_sAleksandar Veljkovic 更新时间:3/17/2023 访问量:52

问:

System.InvalidOperationException:“超时已过期。超时 在从池中获取连接之前经过的时间段。这 可能是因为所有池连接都在使用中,并且最大值 达到了游泳池的大小。

我正在尝试将现有用户名添加到错误集合中,并开始出现此异常...

我在这个班级中遇到异常:

public class UsersCollection: ObservableCollection<Users>   
{  
    public static UsersCollection GetAllUserNames()
    {
        UsersCollection users = new UsersCollection();
        Users user = null;

        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
            conn.Open();  //I GET EXCEPTION ON THIS LINE.

            SqlCommand command = new SqlCommand("SELECT UserName FROM Users", conn);

            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    user = Users.GetUserNamesFromResultSet(reader);
                    users.Add(user);
                }
            }
        }

        return users;
    }
}

包含导致异常的代码的类:

public class Users : INotifyPropertyChanged, INotifyDataErrorInfo
{
    private int _id;
    private string _username;
    private string _password;
    private bool _isAdmin;

    private Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>();

    public event PropertyChangedEventHandler PropertyChanged;
    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

    public void OnPropertyChanged(PropertyChangedEventArgs e) 
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }

    public string UserName
    {
        get { return _username; }
        set
        {
            if (_username == value)
            {
                return;
            }
            _username = value;

            List<string> errors = new List<string>();
            bool valid = true;

            if (value == null || UserName.Length < 5)
            {
                errors.Add("Username can not have less than 5 characters.");
                SetErrors("UserName", errors);
                valid = false;
            }

            UsersCollection collection = new UsersCollection();  
            collection = UsersCollection.GetAllUserNames();       
                                                                     
            for (int i = 0; i < collection.Count; i++)           
            {                                                    
                if (UserName == (object)collection[i])           
                {                                                
                    errors.Add("Username already exist.");       
                    SetErrors("UserName", errors);               
                    valid = false;                               
                }                                                
            }                                                    
            
            if (!Regex.Match(value, @"^\w+$").Success)
            {
                errors.Add("Username can only contain letters, numbers and underscore characters.");
                SetErrors("UserName", errors);
                valid = false;
            }

            if (valid)
            {
                ClearErrors("UserName");
            }

            OnPropertyChanged(new PropertyChangedEventArgs("UserName"));
        }

        private void SetErrors(string propertyName, List<string> propertyErrors)
        {
            errors.Remove(propertyName);
            errors.Add(propertyName, propertyErrors);

            if (ErrorsChanged != null)
            {
                ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
            }    
        }

        private void ClearErrors(string propertyName)
        {
            errors.Remove(propertyName);
  
            if (ErrorsChanged != null)
            {
                ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
            }
        }
  
        public static Users GetUserNamesFromResultSet(SqlDataReader reader)
        {
            Users user = new Users((string)reader["UserName"]);
            return user;
        }
    }
}

添加这些行后,我开始收到异常(我不确定代码是否会按照我的意愿执行,我无法测试它):

UsersCollection collection = new UsersCollection();  
collection = UsersCollection.GetAllUserNames();       
                                                     
for (int i = 0; i < collection.Count; i++)           
{                                                    
    if (UserName == (object)collection[i])           
    {                                                
        errors.Add("Username already exist.");       
        SetErrors("UserName", errors);               
        valid = false;                               
    }                                                
} 

我该如何解决这个问题?

C# WPF 异常 MVVM

评论

0赞 Selvin 2/18/2023
代码审查评论:当你在下一行重新分配它时,有什么用?...这个类没有附加值有什么意义(静态方法可以在任何类中并返回......还有一个 Dapper..collection = new UsersCollection();UsersCollection: ObservableCollection<Users>ObservableCollection<Users>
0赞 Selvin 2/18/2023
你也应该处理sql命令...如果错误仍然存在,则问题可能出在其他方面......Fx 您不会使用相同的连接字符串在其他地方释放 SQLconnection/swl 命令
0赞 Selvin 2/18/2023
也没有意义,因为它只是在比较引用,因为你没有定义相等性UserName == (object)collection[i]

答:

-1赞 Ahmad Itani 2/18/2023 #1

我从您的问题中了解到的是,您正在尝试连接到一个池,并且从他的池中尝试从用户表中选择一个数据,但遗憾的是,您遇到了池问题,因为它已达到最大大小或限制 mmm,如果您的连接字符串正确,请尝试使 cmd.Timeout = 0;会一次又一次地尝试重新连接,直到您能够进入游泳池。