如何遍历数据表中的记录

如何遍历数据表中的记录

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

问题描述

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

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]

推荐答案

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).


<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


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

08-12 14:17