Was ich habe, ist meine Combobox auf Form1.cs [Design], und ich schuf eine separate Klasse namens SQLOperations meine SQL-Zeug zu betreiben, wie übergebe ich an die Combobox ?C# Winforms Hinzufügen von Elementen aus einer Combobox zu einer separaten Klasse
public static void SQLServerPull()
{
string server = "p";
string database = "IBpiopalog";
string security = "SSPI";
string sql = "select server from dbo.ServerList";
SqlConnection con = new SqlConnection("Data Source=" + server + ";Initial Catalog=" + database + ";Integrated Security=" + security);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
//this below doesnt work because it can't find the comboBox
comboBox1.Items.Add(dr[0].ToString());
//the rest of the code works fine
}
con.Close();
}
Warum gibt Ihre Methode kein List- oder String-Array zurück und füllt die Combobox daraus? – Tester101
Ich denke, ich könnte, aber es gibt wirklich keine Möglichkeit, es direkt zu übergeben? –
Natürlich gibt es das, aber möchten Sie Ihre Datenzugriffsebene wirklich an UI-Typen koppeln? – Oded