简体   繁体   English

c#odbcDatareader获取重复的字段值

[英]c# odbcDatareader get the duplicated field value

I have a strange problem: 我有一个奇怪的问题:

Database: Firebird Connection String: Driver={Firebird/InterBase(r)driver};Dbname=xxx;CHARSET=NONE;UID=xxx; 数据库:Firebird连接字符串:Driver = {Firebird / InterBase(r)driver}; Dbname = xxx; CHARSET = NONE; UID = xxx; PASSWORD=xxx 密码= xxx

I use a set of ODBC classes to operation(select) the database table 我使用一组ODBC类来操作(选择)数据库表

when I loop the db records with OdbcDataReader.GetValue(), if some fields(char type) have no value(char_length()=0), it would get the last record field value; 当我使用OdbcDataReader.GetValue()循环db记录时,如果某些字段(char类型)没有值(char_length()= 0),它将获得最后一个记录字段值; if fields has the value, it's ok(does not get the value from last record) 如果字段具有值,就可以了(不会从最后一条记录中获取值)

My code likes below: 我的代码如下:

var dr = this.SqlExecutor.Open(sql);  //sql is String variable that stored the sql statement  
while (dr.Read())  
{  
   this.Logger.Info("-----Customer_Id: " + this.SqlReader.GetFieldAsString(dr, "Customer_Id") + " -----"); // this not duplicated because it's not empty 
   this.Logger.Info("-----Customer_Email: " + this.SqlReader.GetFieldAsString(dr, "Customer_email") + " -----"); //this would if some records has empty value 
}  

// the method SqlExecutor.Open(sql) and SqlReader.GetFieldAsString() please refer to below: //方法SqlExecutor.Open(sql)和SqlReader.GetFieldAsString()请参考以下内容:

    public IDataReader Open(string sql)
    {
        this.Logger.Debug("sql: " + sql);
        if (this.reader != null && !this.reader.IsClosed)
        {
            this.reader.Close();
            this.reader = null;
        }

        try
        {
            this.cmdForSelect.Connection = this.conn;
            this.cmdForSelect.CommandTimeout = 120;
            this.cmdForSelect.CommandText = sql;

            this.cmdForSelect.Parameters.Clear();
            foreach (var p in this.dbParameters)
            {
                this.cmdForSelect.Parameters.Add(p);
            }

            this.reader = cmdForSelect.ExecuteReader();
        }
        catch (Exception ex)
        {
            this.Logger.Error("There is an error: {0}", ex.Message);
            this.Logger.Info("Error sql query:" + sql);
            throw;
        }
        finally
        {
            this.ClearParameters();
        }

        return this.reader;
    }


    public string GetFieldAsString(IDataReader dr, string fieldName)
    {
        try
        {
            var value = dr.GetValue(dr.GetOrdinal(fieldName));

            if (value == DBNull.Value)
            {
                return string.Empty;
            }

            return Convert.ToString(value);
        }
        catch
        {
            return string.Empty;
        }
    }

Besides, this case is fine on my computer, just happened on my customer's computer, I feel this does not matter with mycode, anyone know this, please help me, thanks a lot!!! 此外,这种情况在我的计算机上很好,只是在客户的计算机上发生过,我觉得这与mycode无关,任何人都知道这一点,请帮帮我,非常感谢!!!

Assuming the actual code is not posted in your question; 假设实际代码未发布在您的问题中; I think the problem is with re-initiation of the variable in the actual code. 我认为问题在于实际代码中变量的重新初始化。 You need to revisit the code and check the IF-ELSE condition you have applied on the this.SqlReader.GetFieldAsString(dr, "Customer_email") 您需要重新访问代码并检查已在this.SqlReader.GetFieldAsString(dr,“ Customer_email”)上应用的IF-ELSE条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM