如何使用ado.net从数据库行中获取值 [英] how to get the value from database row wise using ado.net

查看:91
本文介绍了如何使用ado.net从数据库行中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发测试引擎,我希望从Table一张一张地获得记录.

请帮我解决这个问题.

下面的代码返回表的最后一条记录.

I am trying to develop Test engine and i want to get record one by one from Table.

Please help me to solve this problem.

Below code return last record of the table.

OleDbConnection con = new OleDbConnection();
            con.ConnectionString = ConfigurationSettings.AppSettings["connection"];
            con.Open();
            OleDbCommand cmd = new OleDbCommand("Select * from ExamPaper where TestName = @tn", con);
            cmd.Parameters.Add(new OleDbParameter("@tn", cmbtestname.Text));
            OleDbDataReader dr = cmd.ExecuteReader();
            cmbtestname.Items.Clear();

           
                while (dr.Read())
                {
                    txtquestion.Text = Convert.ToString(dr[4]);
                    radioBtn1.Text = Convert.ToString(dr[5]);
                    radioBtn2.Text = Convert.ToString(dr[6]);
                    radioBtn3.Text = Convert.ToString(dr[7]);
                    radioBtn4.Text = Convert.ToString(dr[8]);
                }

推荐答案

您的数据读取器具有所有记录.但每次每行都显示在文本框中但被下一行覆盖.

使用gridView显示所有记录.将数据阅读器绑定为gridview数据源
your datareader have all records. but everytime each rows are displayed in textboxes but overwritten by next row.

Use gridView to display all the records. Bind data reader as gridview datasource


这简直就是我的脑海.尝试从数据读取器中填充一个
this is just off the top of my head. Try populating a
List<>

,并使用LINQ扩展Skip()和Take()在下一个上一个按钮单击事件中一次移动一个项目.在这种情况下,起始页面索引将为零,页面大小将为1.

因此,类似:

from the datareader and the use LINQ extensions Skip() and Take() to move one item at a time on next prev button click event. In this case, starting page index would be zero and page size would be 1.

So, something like:

string item = list.Skip(currentpageindex*pagesize).Take(pagesize).Select(x=>x).FirstOrDefault()



应该管用.您必须确保页面索引是全局变量或静态变量,可以在页面切换之间保留其最新值.

希望对您有所帮助.



should work. You would have to make sure that the page index is a global or a static variable that would retain its most recent value between page switches.

Hope this helps.


如果您只希望返回的查询中包含一行,请更改查询本身.尝试以下查询:
If you want only one row from your returned query then change the query itself. Try this query:
Select DISTINCT * from ExamPaper where TestName = @tn



或在您的while循环中读取第一行后放入break语句:



Or put a break statement after reading first row in your while loop:

while (dr.Read())
{
    txtquestion.Text = Convert.ToString(dr[4]);
    radioBtn1.Text = Convert.ToString(dr[5]);
    radioBtn2.Text = Convert.ToString(dr[6]);
    radioBtn3.Text = Convert.ToString(dr[7]);
    radioBtn4.Text = Convert.ToString(dr[8]);
    break;
}





--Amit





--Amit


这篇关于如何使用ado.net从数据库行中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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