为什么我的代码只读了一行? [英] Why my code only read one row?

查看:99
本文介绍了为什么我的代码只读了一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表有10个récords,当我使用我的方法时只有一个0表示只读取一条记录,因为只有第一行字段为空,另一个字段不为空



查询



My table have 10 récords and when i use my method only have a "0" that means only read one record because only the first row field is null another fields is not nulls

Query

string query = "SELECT * FROM Movies";





我尝试了什么:





What I have tried:

using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {

                        if (string.IsNullOrEmpty(sdr["Code"].ToString()))
                        {
                            C_E = "0";
                            this.Update(C_E);
                            //ViewBag.Message = "cero";
                        }
                        else
                        {
                            C_E = sdr["Code"].ToString();
                            this.Update(C_E);
                            // ViewBag.Message = C_E;
                        }


                    }

推荐答案

问题不在于行数读取 - 虽然没有你的数据我们无法检查,电影表中可能只有一行 - 但 C_E 将始终具有相同的值退出循环时 - 从数据中读取的最后一个值。

我不知道你的更新方法有什么用,但是如果你想要汇总数据库中的所有值,您需要执行以下操作:

The problem is not the number of rows that are read - though without your data we can't check that, there may only be one row in the "Movies" table - but that C_E will always have the same value when you exit the loop - the last value read from the data.
I have no idea what your Update method does, but if you want to assemble all the values from your DB you need to do something like this:
using (SqlDataReader sdr = cmd.ExecuteReader())
{
    StringBuilder sb = new StringBuilder();
    while (sdr.Read())
    {
        string temp = sdr["Code"].ToString();
        if (string.IsNullOrEmpty(temp))
        {
            sb.Append("0");
        }
        else
        {
            sb.Append(temp);
        }
        this.Update(temp);
    }
    C_E = sb.ToString();
}


这篇关于为什么我的代码只读了一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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