SqlDataAdaptor why get a different query

Posted by Murat on Stack Overflow See other posts from Stack Overflow or by Murat
Published on 2010-05-15T12:48:02Z Indexed on 2010/05/15 12:54 UTC
Read the original article Hit count: 440

Filed under:
|
|
|

Hi All,

I have a database reader class. But i want to use this class, sometimes SqlDataAdaptor.Fill() method get another query to Datatable.

I look at Query in SqlCommand Instance and my query is correct but returning values from different table?

What is the problem?

This My Code

public static DataTable GetLatestRecords(string TableName, string FilterFieldName, int RecordCount, params DBFieldAndValue[] FilterFields) { DataTable DT = new DataTable(); Type ClassType = typeof(T);

        string SelectString = string.Format("SELECT TOP ({0}) * FROM {1}", RecordCount, TableName);

        if (FilterFields.Length > 0)
        {
            SelectString += " WHERE 1 = 1 ";

            for (int index = 0; index < FilterFields.Length; index++)
                SelectString += FilterFields[index].ToString(index);
        }

        SelectString += string.Format(" ORDER BY {0} DESC", FilterFieldName);

        try
        {
            SqlConnection Connection = GetConnection<T>();
            SqlCommand Command = new SqlCommand(SelectString, Connection);

            for (int index = 0; index < FilterFields.Length; index++)
                Command.Parameters.AddWithValue("@" + FilterFields[index].Field + "_" + index, FilterFields[index].Value);

            if (Connection.State == ConnectionState.Closed) Connection.Open();
            Command.CommandText = SelectString;
            SqlDataAdapter DA = new SqlDataAdapter(Command);
            DT.Dispose();
            DT = new DataTable();
            DA.Fill(DT);

            if (Connection.State == ConnectionState.Open)
                Connection.Close();
        }
        catch (Exception ex)
        {
            WriteLogStatic(ex.Message);
            throw new Exception(@"Veritabani islemleri sirasinda hata olustu!\nAyrintilar 'C:\Temp' dizini altindadir.\n" + ex.Message);
        }
        return DT;
    }

© Stack Overflow or respective owner

Related posts about sqldatareader

Related posts about fill