这是完成您所要求的原始代码:
string strCmd = "";
string strConn = "";
SqlConnection sqlConn = new SqlConnection();
SqlCommand sqlCmd = new SqlCommand(strCmd, sqlConn);
SqlDataReader sqlRdr = new SqlDataReader();
sqlConn.Open();
if (comboBox1.Items.Count > 0)
comboBox1.Items.Clear();
sqlRdr = sqlCmd.ExecuteReader();
while (sqlRdr.Read())
comboBox1.Items.Add(sqlRdr[0].ToString());
sqlRdr.Close();
sqlConn.Close();
不过,您需要先连接一些东西。第一个是这样的:
string strCmd = ""; // Insert your SQL statement here.
第二:
string strConn = ""; // Your db connection string goes here.
第三:
if (comboBox1.Items.Count > 0) // You don't have to use this. It just checks to see
comboBox1.Items.Clear(); // if there is anything in the combobox and clears it.
最后,由于您正在制作处理表单和数据库之间交互的东西,我强烈建议您使用SqlParameters来防止SQL 注入攻击。