注册 登录
编程论坛 ASP.NET技术论坛

怎么在数据库里查找相同城市的人,然后在listbox里显示啊

晋加答 发布于 2008-04-06 12:51, 2599 次点击

怎么在数据库里查找相同城市的人,然后在listbox里显示啊




     SqlConnection meteor = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["meteor"]);
        meteor.Open();
        string city =this.TextBox1.Text;
        SqlCommand comm = new SqlCommand("select * from studentinformation where native like  '% "+ city +"% '",meteor);

        ListBox1.DataSource = comm.ExecuteReader();
        ListBox1.DataTextField = "studentname";
        ListBox1.DataValueField = "studentnum";
        ListBox1.DataBind();

        meteor.Close();


这样不对吗?为什么没有显示啊

[[it] 本帖最后由 晋加答 于 2008-4-6 13:29 编辑 [/it]]
28 回复
#2
hebingbing2008-04-06 14:11
你是将这段代码放在什么地方啊……
是放在按钮的click事件中吗?还是放在page_load中啊……
如果是在click事件那你要激发这个事件才能显示,要是在page_load中那么你的textbox1里面有值吗……

[[it] 本帖最后由 hebingbing 于 2008-4-6 14:13 编辑 [/it]]
#3
sldtk12008-04-06 14:12
好像你都没声明SqlDataReader对象
SqlDataReader sdr=new SqlDataReader();
sdr=comm.ExecuteReader();
ListBox1.DataSource=sdr;
ListBox1.DataTextField = "studentname";
ListBox1.DataValueField = "studentnum";
ListBox1.DataBind();
meteor.Close();
改成这样试试
#4
晋加答2008-04-06 14:15
我是放在Click里的
#5
晋加答2008-04-06 14:15
激发了,也不显示
#6
晋加答2008-04-06 14:21
[bo]以下是引用 [un]sldtk1[/un] 在 2008-4-6 14:12 的发言:[/bo]

好像你都没声明SqlDataReader对象
SqlDataReader sdr=new SqlDataReader();
sdr=comm.ExecuteReader();
ListBox1.DataSource=sdr;
ListBox1.DataTextField = "studentname";
ListBox1.DataValueField = "studentnum" ...


改了以后,有个错,说SqlDataReader没定义构造函数
#7
sldtk12008-04-06 14:28
你应该是删错了什么吧,重新建一个项目,再做一次看看
#8
晋加答2008-04-06 14:34
不会吧,呵呵.
#9
sldtk12008-04-06 14:37
或者改成SqlDataReader sdr=comm.ExecuteReader();试试看
检查一下引入命名空间没。。。

[[it] 本帖最后由 sldtk1 于 2008-4-6 14:38 编辑 [/it]]
#10
晋加答2008-04-06 14:39
晕,还是不行啊,
啥也不出来......

[[it] 本帖最后由 晋加答 于 2008-4-6 14:45 编辑 [/it]]
#11
sldtk12008-04-06 14:41
你把完整代码发出来看看
#12
晋加答2008-04-06 14:42
显示不出来,还是
#13
sldtk12008-04-06 14:58
那应该是数据库的问题吧
#14
晋加答2008-04-06 18:55
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection meteor = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["meteor"]);
        meteor.Open();
        SqlCommand comm = new SqlCommand("select * from studentinformation where native like  '% " + this.TextBox1.Text+ "% '", meteor);

        
        SqlDataReader  sdr = comm.ExecuteReader();
        ListBox1.DataSource = sdr;
        ListBox1.DataTextField = "studentname";
        ListBox1.DataValueField = "studentnum";
        ListBox1.DataBind();
        meteor.Close();      
    }

完整代码就这么多啊
就用一个Botton让 ListBox 显示出搜索的东西就行了
#15
hebingbing2008-04-06 19:08
代码看不出来有什么错误……
先查看你的aspx页的代码有没有出错……
你将where句子删除改成SqlCommand comm = new SqlCommand("select * from studentinformation ", meteor);
试试看看能不能显示……要显示了那不就将问题锁定了吗?
这样试着修改其他的地方来找出你的错误……
#16
晋加答2008-04-06 19:40
改了就可以了哦

那应该就是where 后面那个有问题吧

呵呵.改啊改
#17
晋加答2008-04-06 22:22
不知道怎么改了,试了好多都不行.怎么办?

帮帮我吧
#18
sldtk12008-04-06 23:28
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection meteor = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["meteor"]);
        meteor.Open();
        SqlCommand comm = new SqlCommand("select * from studentinformation where native =" + this.TextBox1.Text , meteor);

        
        SqlDataReader  sdr = comm.ExecuteReader();
        ListBox1.DataSource = sdr;
        ListBox1.DataTextField = "studentname";
        ListBox1.DataValueField = "studentnum";
        ListBox1.DataBind();
        sdr.Close();
        meteor.Close();      
    }
#19
sldtk12008-04-06 23:33
如果还是不行的话,你将这段代码放到Page_Load中
程序代码:

if(!this.IsPostBack)
{
    SqlConnection meteor = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["meteor"]);
        meteor.Open();
        SqlCommand comm = new SqlCommand("select * from studentinformation", meteor);
        SqlDataReader  sdr = comm.ExecuteReader();
        ListBox1.DataSource = sdr;
        ListBox1.DataTextField = "studentname";
        ListBox1.DataValueField = "studentnum";
        ListBox1.DataBind();
        sdr.Close();
        meteor.Close();      
}
#20
sldtk12008-04-06 23:41
还有一个问题,你的Web.config文件里是怎么配置的,把代码发出来看看
SqlConnection meteor = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["meteor"].ConnectionString);
在Web.config文件中的<configuration>下添加
<connectionStrings>
    <add name="meteor" connectionString="data source=localhost;user id=sa;pwd=sa;database=(你的数据库名)" providerName="System.Data.SqlClient"/>
</connectionStrings>
#21
晋加答2008-04-07 09:40
啊?我是在这里添加的呢.听同学说在这里面加的
<appSettings>
  <add key="meteor" value="server=localhost;database=manage;user=sa;pwd=;"></add>
  </appSettings>

昨天好象是查询语句不对.我这个不能用 = ,要用like ,
#22
晋加答2008-04-07 09:45
顺便再问你一下,我用了2个DropDownList绑定年,月,要在数据库查询与这
2个DropDownList 选择的年,月相同的,要怎么 表达啊 ?
select from 表 where  birth like  ........
后面不知道了
#23
仰望星空2008-04-07 09:51
"select from 表 where  birth >= " + ddlYear.SelectValue + "-" + ddlMonth.SelectValue + "-" + "01 " +
"and birth < " + ddlYear.SelectValue + "-" + (Convert.ToInt32(ddlMonth.SelectValue) + 1) + "-" + "01 "
#24
晋加答2008-04-07 10:54
我照你做了,可是系统老说,无法将varchar格式的1986-12-10转换为int型的
可是在sql里我是用的datetime类型的啊
怎么会这样呢
#25
wang2325042008-04-07 10:58
回复
你定义一个数据集  dataset  用适配器 填充数据集  然后在绑定  一定能成功
#26
晋加答2008-04-07 11:18
啊?
不是很会.唉
#27
晋加答2008-04-07 13:15
这样还是不行啊
会不会还是那个查询语句哪里错了啊  
protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection meteor = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["meteor"]);
        meteor.Open();


        DataSet ds1 = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter("select studentname,year  from studentinformation  where year >=" + DropDownList1.SelectedValue + "-" + DropDownList2.SelectedValue + "-" + "01" + "and year < " + DropDownList1.SelectedValue + "-" + (Convert.ToInt32(DropDownList2.SelectedValue) + 1).ToString() + "-" + "01", meteor);
        sda.Fill(ds1, "yearmonth2");
        GridView1.DataSource = ds1.Tables["yearmonth1"];
        GridView1.DataBind();

        meteor.Close();  
    }
#28
仰望星空2008-04-07 13:39
别忘了加单引号,没想到我写什么你就用什么啊,呵呵
#29
晋加答2008-04-08 10:04
加了单引号,也还是没办法,说
 
运算符" - " 无法应用与string和string 类型的操作数
1