如何遍历数据表中的记录? [英] How to loop through records in a datatable?

查看:74
本文介绍了如何遍历数据表中的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历数据表的记录,并从数据表的每一列中检索数据并填充文本框.
我想通过单击按钮浏览记录.
我已经尝试过了:但它没有用..我的错误在哪里?

I want to loop through the records of the datatable and retrieve data from the each column of the datatable and fill the textboxes.
I want to move through records by a button click.
I have tried this: BUT IT DOESNT WORK.. WHERE IS MY ERROR?

protected void btn_next_Click(object sender, EventArgs e)
       {
           if( inc !=MaxRows-1)
           {
           inc ++;
               navigo();
           }

       }

 protected void Page_Load(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString =" Provider=SQLOLEDB; Data Source=; Initial Catalog=; User ID=; Password=";
            ds = new DataSet();
           
            
             string sql = "SELECT * From Customer";
 
             da = new System.Data.SqlClient.SqlDataAdapter( sql, con );

            con.Open();
            da.Fill( ds, "Customer" );
            navigo();
                 MaxRows = ds.Tables["Customer"].Rows.Count;


            con.Close();
            con.Dispose();
        
        }
        private void navigo()
        {
       
            DataRow dRow = ds.Tables["Customer"].Rows[inc];
            txt_code.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();
        txt_name.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();
            txt_desc.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();
        }



[edit]代码块已修复[/edit]



[edit]code block fixed[/edit]

推荐答案

尝试以下方法:
Try this:
foreach(DataRow dr in DataTable)
{
   if(dr[CustomerID].ToString()=="101")
       TextBox1.Text="Customer101";
}


报价:

txt_code.Value = ds.Tables ["Customer"] .Rows [inc] .ItemArray.GetValue(inc).ToString();
txt_name.Value = ds.Tables ["Customer"].Rows [inc] .ItemArray.GetValue(inc).ToString();
txt_desc.Value = ds.Tables ["Customer"].Rows [inc] .ItemArray.GetValue(inc).ToString();

txt_code.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();
txt_name.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();
txt_desc.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();



我想你应该改用



I think you should use instead

txt_code.Value = ds.Tables["Customer"].Rows[inc][0].ToString();
txt_name.Value = ds.Tables["Customer"].Rows[inc][1].ToString();
txt_desc.Value = ds.Tables["Customer"].Rows[inc][2].ToString();


其中{0,1,2}是您需要的列的索引(适当更改).


where {0,1,2} are the indices of the columns you need (change as appropriate).


您好,


我想我可以看到发生了什么事,
但是我不太擅长C/Java

因此,我将尝试解释一下,然后在VB中为您提供示例

基本上,您的Inc变量不会保存位置,因为它没有PAGE范围,
是的,您可以将其保存在会话范围变量中,但是最终您不希望每次更改行时都重新加载表!

因此,您的某些变量需要在PAGE范围内进行设置,而其他变量则可以保留在子例程/过程范围内.

强制性地将所有变量声明为DECLARE的良好做法
在visuabl basic中,我们通过在页面顶部声明OPTION EXPLICIT来实现此目的...

然后,当您单击NEXT/PREV按钮时,按钮的例程仅从给定页面范围的数据集中提取数据.


我不经常使用数据集并且更喜欢sqlcommand,但这是个人选择,提及此原因是因为我不确定我是否正确编写了代码,但是下面的代码并不应该太难看到差异在我对您的代码的调用与您对代码的调用之间.

Hello,


I think i can see whats going on,
but i''m not very good at C/Java

So I will try to explain and then give you a sample in VB

Basically your Inc variable is not saving the position because its not got PAGE scope,
Yes you can save it in a session scope variable, but ultimately you DONT want to reload the table everytime you change the row!

So some of your variables need setting at the PAGE scope and others can stay in the subroutine/procedure scope.

its ALWAYS good practice to DECLARE ALL variables as a compulsory step
in visuabl basic we do this by declaring OPTION EXPLICIT at the TOP of the page before ANYTHING else...

And then when you click your NEXT/PREV buttons the routine for the buttons simply pulls the data fromthe dataset which is given page scope.


I dont often use dataset and am more comfortable with an sqlcommand but thats a personal choice and the reason for mentioning this is because im not sure i''ve coded it right but the code is below and it shouldnt be too difficult to see the differences between my invocation of your code and your invocation.

<script runat="server">

	Dim Increment As Long
	Dim MaxRows As Long
	Dim DataSett As System.Data.DataSet
	

	Protected Sub Page_Load(sender As Object, e As System.EventArgs)
		Dim sqlConn As Data.SqlClient.SqlConnection
		Dim sqlAdapter As Data.SqlClient.SqlDataAdapter
		Dim sqlQuery As String
		''
		sqlConn = New System.Data.SqlClient.SqlConnection()
		sqlConn.ConnectionString = "Provider=''SQLOLEDB''; Data Source=; Initial Catalog=; User ID=; Password=;"
		sqlQuery = "SELECT * From Customer"
		sqlAdapter = New System.Data.SqlClient.SqlDataAdapter(sqlQuery, sqlConn)
		sqlConn.Open()
		sqlAdapter.Fill(DataSett, "Customer")
		MaxRows = DataSett.Tables("Customer").Rows.Count
		sqlConn.Close()
		sqlConn.Dispose()
		''tidy up
		sqlConn = Nothing
		sqlAdapter = Nothing
		sqlQuery = Nothing
	End Sub
	Protected Sub Page_Unload(sender As Object, e As System.EventArgs)
		''tidy up
		Increment = Nothing
		MaxRows = Nothing
		DataSett = Nothing
	End Sub
	
		
	Protected Sub btn_prev_Click(sender As Object, e As System.EventArgs)
		If (Increment > 0) Then
			Increment = Increment - 1
			navigo()
		End If
	End Sub
	
	Protected Sub btn_next_Click(sender As Object, e As System.EventArgs)
		If (Increment <> MaxRows - 1) Then
			Increment = Increment + 1
			navigo()
		End If
	End Sub

	Private Sub navigo()
		Dim dRow As System.Data.DataRow
		dRow = DataSett.Tables("Customer").Rows.Item(Increment)
		txt_code.Value = DataSett.Tables("Customer").Rows(Increment).ItemArray.GetValue(Increment).ToString()
		txt_name.Value = DataSett.Tables("Customer").Rows(Increment).ItemArray.GetValue(Increment).ToString()
		txt_desc.Value = DataSett.Tables("Customer").Rows(Increment).ItemArray.GetValue(Increment).ToString()
		''dont forget to tidy up
		dRow = Nothing
	End Sub
		
</script>



最后,别忘了整理一下以避免网页上的内存泄漏



Finally, dont forget to tidy up to avoid memory leaks in your web page


这篇关于如何遍历数据表中的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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