简体   繁体   中英

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; PASSWORD=xxx

I use a set of ODBC classes to operation(select) the database table

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; 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:

    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!!!

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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