本文介绍了将数据加载到没有空白的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些代码将问题从我的MSSQL服务器加载到组合框中。代码工作,它加载数据,但当我从下拉列表中选择项目时,它会从SQL表中加载所有空白区域。
这是我的代码:
private void loadlocationcombobox()
{
string constr = ConfigurationManager.ConnectionStrings [ SQLServer]。ConnectionString;
string query = 从dbo中选择location_name .Location;
SqlConnection con = new SqlConnection(constr);
SqlCommand command = new SqlCommand(query,con);
con.Open();
SqlDataReader sqlreader = command.ExecuteReader();
尝试
{
while (sqlreader.Read())
{
comboLocation.Items.Add(sqlreader [ 0 ]);
}
}
catch (Exception ex)
{
MessageBox.Show( 发生错误: + ex.Message, error,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
最后
{
sqlreader.Close();
con.Close();
}
}
任何帮助都会很棒,因为我一直都很棒查看代码,但无法看到如何修复它。
解决方案
Hi,
im have a problem with some code to load data from my MSSQL server in to a combo box. the code works and it loads the data but when i select the item from the drop down it loads all the blank space from the SQL table aswell.
this is my code:
private void loadlocationcombobox() { string constr = ConfigurationManager.ConnectionStrings ["SQLServer"].ConnectionString; string query = "select location_name from dbo.Location"; SqlConnection con = new SqlConnection(constr); SqlCommand command = new SqlCommand(query, con); con.Open(); SqlDataReader sqlreader = command.ExecuteReader(); try { while (sqlreader.Read()) { comboLocation.Items.Add(sqlreader[0]); } } catch (Exception ex) { MessageBox.Show("A error has occurred: " + ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { sqlreader.Close(); con.Close(); } }
any help would be great as i have been looking at the code but just cant see how to fix it.
解决方案
这篇关于将数据加载到没有空白的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!