阅读器未读取数据 [英] Reader is not reading the data

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

问题描述

我有这样的功能

try
    {


        using(var sConnection = new SqlConnection(ConnectionString))
        using(var sCommand = sConnection.CreateCommand())
        {
            sCommand.CommandText = @"SELECT 
                                           TABLE_NAME
                                          AS
                                           TABLES
                                        FROM 
                                           INFORMATION_SCHEMA.TABLE_CONSTRAINTS
                                       WHERE 
                                           CONSTRAINT_TYPE = 'PRIMARY KEY'
                                         AND
                                           TABLE_NAME <> 'dtProperties'
                                    ORDER BY
                                           TABLE_NAME";
            sConnection.Open();
            using(var reader = sCommand.ExecuteReader()) // Troublesome line
            {                    
                while(reader.Read())
                {
                    sb.AppendLine(reader.GetString(0));
                }
            }
        }

    }
    catch(Exception ex)
    {
        //All the exceptions are handled and written in the EventLog. 
        EventLog log = new EventLog("Application");
        log.Source = "MFDBAnalyser";
        log.WriteEntry(ex.Message);
    }
    return sb.ToString();
}

在调试时,它会给出结果,直到连接打开但 var Reader 未读取数据.

On debugging, it is giving the result till the connection is open but the var Reader is not reading the data.

谁能指出错误在哪里!!

Can anybody point out where the error is!!

推荐答案

你提供的代码和你运行的一样吗?

Is your provided code the same you run?

工作"在调试中的奇怪行为,而不是在运行时,如果你有自初始化 get 属性,通常会发生.像这样:

Strange behaviour of 'working' in debug, but not in runtime, usually happens if you have self initializing get properties. Something like this:

private string _name;

public string Name
{
  get {
    if (_name == null)
    {
      name = string.Empty;
    }
    return _name;
  }
  set {_name = value;}
}

因此,如果您直接使用 _name 并且调试器显示这样的属性,它会在使用前意外"初始化您的属性.

So if you use _name directly and debugger displays such a property, it 'accidentally' initializes your property before use.

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

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