using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace Window1
{
public partial class Shopping : Form
{
public Shopping()
{
InitializeComponent();
} private void Shopping_Load(object sender, EventArgs e)
{
string connString = "server=WIN-8B49A4OVKBA\\SQLEXPRESS;database=Shop;uid=sa;pwd=19870409";
SqlConnection connection = new SqlConnection(connString);
string sql = string.Format("select type from shopping");
try
{
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader datareader = command.ExecuteReader();
string type1;
while (datareader.Read())
{
type1 = (string)datareader["type"];
cboType.Items.Add(type1);
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
}
}
}
代码没问题,但是我的下拉菜单中显示好几个重复的,怎么解决重复的只显示一个?
因为我写的循环结构,所有所有的shopping表中的type字段的值都显示出来了,有好多重复的,怎么才能让重复的只显示
一个,用if结构判断么?条件怎么写?谢谢大家了
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace Window1
{
public partial class Shopping : Form
{
public Shopping()
{
InitializeComponent();
} private void Shopping_Load(object sender, EventArgs e)
{
string connString = "server=WIN-8B49A4OVKBA\\SQLEXPRESS;database=Shop;uid=sa;pwd=19870409";
SqlConnection connection = new SqlConnection(connString);
string sql = string.Format("select type from shopping");
try
{
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader datareader = command.ExecuteReader();
string type1;
while (datareader.Read())
{
type1 = (string)datareader["type"];
cboType.Items.Add(type1);
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
}
}
}
代码没问题,但是我的下拉菜单中显示好几个重复的,怎么解决重复的只显示一个?
因为我写的循环结构,所有所有的shopping表中的type字段的值都显示出来了,有好多重复的,怎么才能让重复的只显示
一个,用if结构判断么?条件怎么写?谢谢大家了
解决方案 »
- 如何获取粘贴文件后的路径
- 在listview中怎么筛选
- 有web关对话框的问题,似乎很难搞,高手帮帮忙吧,困扰我两个星期了。
- 数据连接问题
- 》》》晕死,asp.net2.0老是w3wp.exe异常,记录在事件日志里了,有详细,高手进。。。。
- 求我的编程生涯一文
- 这个表怎么生成!!!
- 为什么我的更新不行呀?
- 急!急!急!!!VC++下的代码移植到.net的C#中的问题!!!
- winform中tabControl选项卡如何纵向排列
- 关于SQL数据库日期的问题
- System.Data.DBConcurrencyException: 违反并发性: UpdateCommand 影响了预期 1 条记录中的 0 条的解决方法
while (datareader.Read())
{
type1 = (string)datareader["type"];
cboType.Items.Add(type1);
}