没有获得所有值只获得第一个值集 [英] not get all values only gets first values set

查看:92
本文介绍了没有获得所有值只获得第一个值集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SQL管理工作室和C#

所以我需要从数据库中获取值并连接新值并存储在数据库中

示例

过去的价值=aaaaa

新值=bbbbb

需要存储价值=aaaaa / bbbbb这样



所以我尝试这样但只得到这样的最后一个值

Ex

数据库中的最后一个酸疼像这样的ccc / bbb

应用程序只获取ccc,但我希望获得所有价值ccc / bbb



这是我的代码获取值





I use SQL management studio and C#
So I need to get value from database and concatenate with new value and store in database
example
past value = "aaaaa"
New value = "bbbbb"
Need to store value = "aaaaa / bbbbb" like this

So I try like this but only get the last value like this
Ex
Last value sore in the database like this "ccc / bbb"
Application only get ccc but i want get all value "ccc / bbb"

this is my code get values


string pastval , past;

using (SqlConnection conn = new SqlConnection(@"Data Source=MCK-PC\MCKSQL;Initial Catalog=application_database;Integrated Security=True"))
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand("SELECT * FROM tbl1 WHERE id=@id", conn);
                    command.Parameters.AddWithValue("id", txt_id.Text);
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        pastval =reader["name"].ToString();
                        past= txtnew_name.Text +" / "+pastval.ToString();

                    }
                    reader.Close();
                    conn.Close();
                }



提前致谢!


Thanks in advance !

推荐答案

您需要附加文字:

You need to append the text:
StringBuilder sb = new StringBuilder(textnew_name.Text);
while (reader.Read())
{
    sb.AppendFormat(" / {0}", reader["name"])
}
past = sb.Tostring();


Quote:




SqlCommand command = new SqlCommand("SELECT * FROM tbl1 WHERE id=@id", conn);



你有答案,但我想在这里提一点。



你写的 SELECT *,它将选择表中的所有列并且非常耗时。

但是从代码中可以看出,您只需要列 name ,如你所写的那样 reader [name]



所以,你应该cha将查询发送到...


You have got your answer, but I would like to mention one point here.

You have written SELECT *, which will select all Columns of the Table and is very time consuming.
But from the code, it is clear that you only need the Column "name" as you have written like reader["name"].

So, you should change the Query to...

SqlCommand command = new SqlCommand("SELECT name FROM tbl1 WHERE id=@id", conn);

这篇关于没有获得所有值只获得第一个值集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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