C#代码在Windows窗体的列表框中显示查询结果 [英] C# code to Display a Query Result in a List Box in windows forms
问题描述
亲爱的所有人
我正在开发需要在列表框中显示搜索查询结果的软件.我的是医疗软件,正在尝试在列表框中显示药品列表.我在Windows Visual Studio 10平台下使用c#进行编码.请帮助我.
我使用Sql Server Management Studio作为后端.即时通讯使用一个按钮,单击该按钮将触发执行查询的事件.查询是从Table_name中选择*我想在列表框中显示查询列表..
问候
haldin
Dear All
I am working on a software in which i need to display the search query results in a listbox. mine is a medical software and im trying to display the list of medicines in the listbox. im using c# for coding under Windows Visual studio 10 platform. please help me wid this.
Im using Sql server management studio as backend. im using a button which when clicked will fire an event performing a query. the query is select * from Table_name i want to show the query list in the listbox..
regards
haldin
推荐答案
private void button1_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Server=.;Integrated Security=true;...";
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "Select * From Table_Name";
command.CommandType = CommandType.Text;
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
string title = (string)reader["Title"];
string description = (string)reader["description"];
string item = string.Format("{0} - {1}", title, description);
this.listBox1.Items.Add(item);
}
reader.Close();
}
catch
{
}
finally
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
}
这篇关于C#代码在Windows窗体的列表框中显示查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!