如何从具有8个值的特定列一次读取所有值 [英] How Can I Read All Values At One Time From A Particular Column Having 8 Values

查看:94
本文介绍了如何从具有8个值的特定列一次读取所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想读取一列中所有行的值,使用datareader进行检索



像datareader一次只显示一行值,然后是第二行等等。



i想要立即读取所有值并将这些值分配给其他变量就是这样。

I just want to read value's of all rows in one column which is retrevied using datareader

like datareader show only one row value at one time and then second and so on.

i want to read all value's at once and assing these values to other varriable's that's it.

推荐答案

您好,



我假设您要从多行中检索列值作为连接字符串。如果这是场景,那么在SQL Server中您可以使用以下存储过程来实现。

Hello,

I am assuming that you want to retrieve the column value from multiple rows as a concatenated string. If this is the scenario then in SQL Server you can do so by using following Stored Proc.
CREATE FUNCTION getRows() AS
BEGIN
    DECLARE @Names VARCHAR(8000)

    SELECT @Names = '';
    SELECT @Names = @Names + ',' + ISNULL(UserName, 'N/A')
    FROM users

    RETURN @Names;
END;



以下代码段显示了如何从客户端调用此proc。


Following code snippet shows how you can call this proc from your client.

SqlConnection sqlCon = new SqlConnection("YOUR CON STR");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;

cmd.CommandText = "getRows";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlCon;

sqlCon.Open();

reader = cmd.ExecuteReader();
// Code to Access the data


sqlConnection1.Close();



问候,


Regards,


您可以使用数据表或数据集一次读取值集合。



参考例:



sqlConnection = new SqlConnection(ConnString);

string sql =你的查询;

SqlCommand命令=新的SqlCommand(sql,sqlConnection);

SqlDataAdapter da = new SqlDataAdapter(command);

da.Fill(dt);
You can use data table or dataset to read collection of values at once.

Ref Example:

sqlConnection = new SqlConnection(ConnString);
string sql = "Your Query";
SqlCommand command = new SqlCommand(sql, sqlConnection);
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);


这篇关于如何从具有8个值的特定列一次读取所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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