在我創建的黑客組織遊戲的wpf應用程序中訪問列表中的內容時出現問題。在WPF應用程序中從SQL Server中選擇數據
基本上我從SQL Server 2016讀取,然後將其添加到我的用戶集合列表中。後來當我在顯示器中使用我的列表時,他們都在那裏。
但是,當我使用foreach循環來設置一個臨時用戶等於一個找到的用戶名時,它只能找到硬編碼的用戶,而不是使用從SQL Server讀取的數據添加的用戶。需要幫忙。
SQL Server讀取的代碼:
using (connect = new SqlConnection(connetionString))
{
connect.Open();
string readString = "select * from Users";
SqlCommand readCommand = new SqlCommand(readString, connect);
using (SqlDataReader dataRead = readCommand.ExecuteReader())
{
if (dataRead != null)
{
while (dataRead.Read())
{
tempEmail = dataRead["Email"].ToString();
tempName = dataRead["Name"].ToString();
UserCollection.addUser(tempEmail, tempName);
}
}
}
connect.Close();
}
UserCollection
相關部分
private static List<User> UserList = new List<User>();
// add a user
public static void addUser(string email, string name)
{
UserList.Add(new User(email, name, 0, "unset", false, false, false, false, false, false,
false, "", false, false, false, 0, 0));
}
//return list of users for use elsewhere
public static List<User> ReturnUserList()
{
return UserList;
}
使用列表的設置tempPlayer在WPF窗口的代碼,其中的列表
PlayersList = UserCollection.ReturnUserList();
// tempPlayer = UserCollection.ReturnAUser(sessionUser);
foreach (var element in PlayersList)
{
if (element.UserName == sessionUser)
{
tempPlayer = element;
}
}
例作品。
// set listing of current players
ListOfPlayers = UserCollection.ReturnUserList();
var tempList = from player in ListOfPlayers
where player.UserBlocked == false
select new
{
Name = player.UserName,
Email = player.UserEmail,
};
this.PlayerListBox.ItemsSource = tempList;
硬編碼的用戶添加工作正常,並且可以通過foreach語句從我app.xaml.cs
UserCollection.addUser("g", "Tom");
這裏有很多東西在這裏沒有顯示,就像任何UserCollection一樣。 – tadman
我可以添加更多,但是整套代碼是1000行+ – LINQtothepast
借調,很難在沒有看到其他課程的情況下發現問題的位置。它看起來像你的實現比它需要更復雜一點 - 使用一個方法來返回你的用戶列表,而不是一個屬性,例如。我們需要的僅僅是一個簡單的例子,因此您在數據庫邏輯中調用的任何變量/方法。我們需要一個可驗證的例子來工作,但不是所有的1000多行。 – Alex