我试图从mysql数据库中获取值,我创建了一个单独的类,以便为我的队友创建一个更好的概述。
在课堂上我有这样的代码:
public void missingOverview()
{
//missing items ophalen
//connectie opzetten
MySqlConnectionStringBuilder connectionStr = new MySqlConnectionStringBuilder
{
Server = "localhost",
Database = "GIPLouisThuyttenGregoireRobbe",
UserID = "root",
Password = "usbw",
ConnectionTimeout = 60,
Port = 3307,
AllowZeroDateTime = true
};
MySqlConnection MySqlCon = new MySqlConnection(connectionStr.ConnectionString);
//connectie aanmaken en query aanvragen
try
{
//checken of de connectiestatus open of gesloten is, en zet deze open indien gesloten
if (MySqlCon.State == System.Data.ConnectionState.Closed)
MySqlCon.Open();
//query aanvragen
String query = "SELECT Username FROM login";
MySqlCommand sqlCmd = new MySqlCommand(query, MySqlCon);
MySqlDataReader reader = sqlCmd.ExecuteReader();
while (reader.Read())
{
app.lsbMissingOverview.Items.Add(reader.GetString("Username"));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
MySqlCon.Close();
}
在我的窗口里有这样的代码:
private void btnOverview_Click(object sender, RoutedEventArgs e)
{
//overview toevoegen
Overview overviewClass = new Overview();
overviewClass.missingOverview();}
当按下按钮时,没有值显示,有人知道为什么吗?
暂无答案!
目前还没有任何答案,快来回答吧!