读取一行数据 [英] Reading a row of data

查看:82
本文介绍了读取一行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.

我想将数据读取器从我的sql数据库读取的一行一行存储到gridview中.

基本上:

而reader.Read()

//这里我想获取它读取的整个行,例如. theName,说明,编号
//并按原样存储它,以便可用于填充我的gridview.

结束而

当您运行类似:
的查询时,gridview必须看起来完全像一个SQL表. 选择*来自tblClients

有帮助吗?

Hi.

I want to store row for row which a datareader reads from my sql database into a gridview.

basically :

While reader.Read()

//Here i want to get the entire row that it reads eg. theName,Description,number
//and store that as is so that it can be used to populate my gridview.

end while

The gridview must look exactly like a SQL table would when you run a query like :
SELECT * FROM tblClients

any help?

推荐答案

为什么?

一个简单的绑定是不可能的选择?

Why?

A simple binding can''t be an option???

public static DataTable GetData()
{
    DataTable table = new DataTable();
    string query = @"SELECT * FROM tblClients";

    using (SqlConnection cn = new SqlConnection(Configuration.DefaultConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(query, cn))
        {
            cn.Open();

            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                table.Load(reader);
            }
        }
    }

    return table;
}

MyGrid.DataSource = GetData();
MyGrid.DataBind();



VB.NET



VB.NET

Public Shared Function GetData() As DataTable
	Dim table As New DataTable()

	Using cn As New SqlConnection("your connection string")
		Using cmd As New SqlCommand("SELECT * FROM tblClients", cn)
			cn.Open()

			Using reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
				table.Load(reader)
			End Using
		End Using
	End Using

	Return table
End Function


干杯


DataReader FieldCount 属性告诉您列数.
GetFieldType(columnIndex)函数提供有关数据类型的信息.
然后通过数据进行嵌套循环,并填充gridview.
The FieldCount property of the DataReader tells you the number of columns.
The GetFieldType(columnIndex) function gives infromation on the type of the data.
And then do the nested loops thru your data, and fill the gridview.


这篇关于读取一行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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