我写了这个方法,这样我就可以使用它而不是重复代码.但是我如何才能将cmb1
(组合框名称)更改为类似cmb(i)
的名称.我的意思是,如果I=10组合框名=cmb10
.对于code(cmb1.Items.Add(srd.GetValue(0).ToString());).i的这一部分,想要将有关i的数字更改为i.可能吗?如果可能,请帮助我!
这就是我正在try 的方法
public void ComboText(string text,int i)
{
try
{
string connectionString = @"Data Source=DESKTOP-V0HE9JH\SQLEXPRESS01;Initial Catalog=Parts_Shop;Integrated Security=True";
SqlConnection connection = new SqlConnection(@connectionString);
connection.Open();
SqlCommand command = new SqlCommand("select manufacturer from Parts where item_type = '" + text + "'", connection);
SqlDataReader srd = command.ExecuteReader();
while (srd.Read())
{
cmb1.Items.Add(srd.GetValue(0).ToString());
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}