我想访问数据库数据,需要在列表视图或任何东西上显示 [英] i want to access database data and need to display on list view or anything

查看:65
本文介绍了我想访问数据库数据,需要在列表视图或任何东西上显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SqlConnection sqlnew2 = new SqlConnection();
       sqlnew2.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=Training;Integrated Security=True";

       sqlnew2.Open();


       SqlCommand cmd = new SqlCommand("select DepId from Department where DepName=@DeptName;", sqlnew2);
       cmd.Parameters.Add(new SqlParameter("@DeptName", DepNameDropDown.SelectedItem.ToString()));
       SqlDataReader sqlread = cmd.ExecuteReader();



       while (sqlread.Read())
       {
           SqlConnection sqlnew3 = new SqlConnection();
           sqlnew3.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=Training;Integrated Security=True";

           sqlnew3.Open();

           string depid = sqlread["DepId"].ToString();

           SqlCommand cmd2 = new SqlCommand("select EmpID,EmpName,EmpAddress from Employee where DepId='" + depid + "'", sqlnew3);



           SqlDataAdapter da = new SqlDataAdapter();

           da.SelectCommand = cmd2;
           DataSet newds = new DataSet();
           da.Fill(newds);
           DataListView.DataSource = newds;
           DataListView.DataBind();



       }





基本上

这里是代码

i在我的数据库中有2个表

一个表持有



Dep ID [prmary key]

Dep name



其他表包含

EmpId [主键]

EmpName

EmpAddress

DepId [外键]



我的cod =首选代码将获得DepId

和depid存储为字符串



第二次查询将比较数据与该字符串

并根据该字符串查看所有数据



我需要在列表框或gridview中查看它



场景是这样的.....

i有一个组合框,当软件启动时,sql数据将加载到组合框

当组合框值选择

所有数据应该在datagridview中查看..



当我点击组合框时出现问题.....

数据不会加载到datagridview



basically
here is the code
i have 2 tables in my data base
one table holds

Dep ID[prmary key]
Dep name

other table contains
EmpId[Primary key]
EmpName
EmpAddress
DepId[Foreign Key]

my cod = first select code will get the DepId
and depid stored as a string

second query will compare the data with that string
and view all the data according to that string

and i need to view it in listbox or gridview

the scenario is like this.....
i have a combo box and when software starts sql data will load to the combo box
when combo box value select
all the data should view in the datagridview..

the problem is when i click the combo box.....
data will not load into the datagridview

推荐答案


看看这篇文章



http: //www.dotnetpools.com/2012/10/how-to-bind-gridview-in-windows.html [ ^ ]



在下面已经展示了如何绑定和进行分页..

http://www.dotnetpools.com/2013/04/code-for-paging-in-gridview-in-aspnet.html [ ^ ]


这篇关于我想访问数据库数据,需要在列表视图或任何东西上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆