问题描述
如何用数据库结果填充listobx.我想从数据库中选择一个ID和一个名称,并使用能够从显示数据库搜索结果的列表框中访问ID.在下面的代码中,我尝试将结果放入数据库中,并从数据表内容填充列表框.但它似乎不起作用.有帮助吗?
how do i populate a listobx with results from a database. I want to select an id and a name from the database and use the be able to access the id from the listbox in which the results of the db search are displayed. in the code below i try to put the results in a database and the populate the listbox from the data table contents. but it doesnt seem to work. any help?
String connection, sql;
connection = Properties.Settings.Default.cvmanagerConnectionString;
sqlA = "select FileLoc, Lname from apps where dept = '" + intdept.Text + "' and role = '" + introle.Text + "' and rating = '" + intratinglevel.Text + "' and yearsofexp = '" + intexp.Text + "' and hnd = '" + edulev.Text + "' ";
//datatable creation for storing search results
DataTable results = new DataTable("searchResults");
DataColumn column;
DataRow row;
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "apps";
column.ReadOnly = true;
column.Unique = true;
results.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "LastName";
column.ReadOnly = true;
column.Unique = true;
results.Columns.Add(column);
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = results.Columns["apps"];
results.PrimaryKey = PrimaryKeyColumns;
cvmanagerDataSet1.Tables.Add(results);
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
reader = cmd.ExecuteReader();
intsearchresults.Enabled = true;
while (reader.Read())
{
row = results.NewRow();
row["apps"] = reader["apps_id"].ToString();
row["LastName"] = reader["Lname"].ToString();
results.Rows.Add(row);
intsearchresults.Items.Add(reader["Lname"].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
reader.Close();
conn.Close();
}
}
[edit]整理一下代码块-OriginalGriff [/edit]
[edit]Just a tidy on the code block - OriginalGriff[/edit]
推荐答案
while (reader.Read())
{
row = results.NewRow();
row["apps"] = reader["apps_id"].ToString();
row["LastName"] = reader["Lname"].ToString();
results.Rows.Add(row);
intsearchresults.Items.Add(reader["Lname"].ToString());
}
读者从未输入名为"apps_id"的列,但您的select语句确实选择了名为"FileLoc"的列.
3.在您的where子句中:
the reader never took in a colomn named ''apps_id'', but your select statement does select a column named ''FileLoc''.
3. In your where clause:
" where dept = ''" + intdept.Text + "'' and role = ''" + introle.Text + "'' and rating = ''" + intratinglevel.Text + "'' and yearsofexp = ''" + intexp.Text + "'' and hnd = ''" + edulev.Text + "'' "
controls(TextBox?)名称似乎表明它们是整数.如果是这种情况,则意味着值以整数形式存储在数据库中,那么您就不想将它们用单引号引起来.
希望对您有所帮助.
the controls(TextBox?) names seem to indicate that they are integers. If this is the case, meaning the values are stored in the database as integers, then you do not want to wrap them in single quote marks.
Hope this helps.
这篇关于我如何用数据库结果填充列表框.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!