views:

33

answers:

0

Hi All,

I have a database reader class. But ı 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<T>(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(@"Veritabanı işlemleri sırasında hata oluştu!\nAyrıntılar 'C:\Temp' dizini altındadır.\n" + ex.Message);
        }
        return DT;
    }