如何从SQL检索数据 [英] how to retrive data from sql
问题描述
在我的应用程序中,我从sql.in数据库中检索数据,该数据包含6条记录,但是当我运行页面时,它仅显示1条记录,其余5条记录不显示.下面我写代码.
in my application i am retrive data from sql.in database contain 6 record but when i run page it display only 1 st record and rest 5 record not display. below i m write code .
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=Aarambh;Initial Catalog=rebuild_technology;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from content_managment where page_id = 1 ", con);
DataSet ds = new DataSet("temp");
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = cmd;
ad.Fill(ds);
DataRow dr = ds.Tables[0].Rows[0];
Session["divhtml_heading1"] = dr["divhtml_heading"].ToString();
Session["divhtml_heading2"] = dr["divhtml_content"].ToString();
}
推荐答案
您已经告诉它只能在SQL语句中检索1条记录...(除非您有6条记录的页面ID为1 ?)
You''ve told it to only retrieve 1 record in your SQL statement...(unless you have 6 records with a page id of 1?)
select * from content_managment where page_id = 1
此外,您只从结果中获取第一行,然后对结果进行操作...
DataRow dr = ds.Tables [0] .Rows [0];
您需要遍历Rows集合,或绑定到网格-取决于您实际上要做什么.
例如
Also, you are only taking the first row from the results and doing something with that...
DataRow dr = ds.Tables[0].Rows[0];
You would need to iterate over the Rows collection, or bind to a grid - depends what you are trying to do really.
e.g.
foreach(DataRow row in ds.Tables[0].Rows)
{
// do something with each row in the results...
}
从content_managment中选择*
select * from content_managment
在此处检查您的代码
Check your code here
DataRow dr = ds.Tables[0].Rows[0];
您仅从数据集中检索第一行.请像下面那样遍历数据集:
You are retrieving only 1st row from the dataset. Please loop through the dataset like below:
if(ds.Tables[0].Rows.Count > 0)
{
foreach(DataRow dr in ds.Tables[0])
{
string strHeading = Convert.ToString(dr["divhtml_heading"]);
string strContent = Convert.ToString(dr["divhtml_content"]);
}
}
还要检查您正在数据库中运行的查询,如果它确实返回6条记录.我问这个问题是因为您在查询中提到了一个条件,
Also check the query you are running in the database if it really returns 6 records. I am asking this as you have mentioned a condition in your query,
select * from content_managment where page_id = 1
这篇关于如何从SQL检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!