我想检查表中的名称是否已在组合框中列出,如果是,请不要插入。如果不插入。我尝试了以下方法:
public void display()
{
try
{
sc.Open();
string Query = "select * from Part";
SqlCommand createCommand = new SqlCommand(Query, sc);
SqlDataReader dr = createCommand.ExecuteReader();
while (dr.Read())
{
string Name = dr.GetString(1);
if (Name != cbPartners.Text)
{
cbPart.Items.Add(Name);//Displaying a list in the Combo Box
}
else
{
cbPart.Items.Clear();
}
}
sc.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}