我有一个数据库项目,需要将数据库中的元素添加到清单框,该怎么做?
这是我编写的代码,但似乎有问题
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string connection_string = @"Data Source=DESKTOP-MMHA4KL;Initial Catalog=Movie rental;Integrated Security=True";
SqlConnection connection = new SqlConnection(connection_string);
string Add_to_checkbox = "SELECT name from [Actor]";
SqlCommand comm = new SqlCommand(Add_to_checkbox, connection);
connection.Open();
SqlDataReader read_name = comm.ExecuteReader();
int lastindex = 0;
while(read_name.Read())
{
string name = read_name.ToString();
checkedListBox1.Items.Add(name);
}
}
答案 0 :(得分:0)
即使在查询中仅选择了一个字段,您仍需要在读取中指定它。尝试
string name = read_name["name"].ToString();