예제 #1
0
        public List <T> readTable <T>(string objectID, int column)
        {
            Type     t         = typeof(T);
            List <T> tableData = new List <T>();
            T        rowData;

            PropertyInfo[] props = t.GetProperties();

            string query = string.Format("select * from {0} " +
                                         "where {1} = '{2}';",
                                         t.Name,
                                         props[column].Name,
                                         objectID);

            try
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                MySqlCommand    cmd    = new MySqlCommand(query, conn);
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    rowData = new InstanceOf <T>().Create();

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        MapValueToType(props[i], rowData, reader, i);
                    }
                    tableData.Add(rowData);
                }
                conn.Close();
                return(tableData);
            }

            catch (MySqlException err)
            {
                Console.WriteLine(err);
                return(null);
            }
        }
예제 #2
0
        public T readRow <T>(T obj, string objectID)
        {
            T rowData = new InstanceOf <T>().Create();

            PropertyInfo[] props = obj.GetType().GetProperties();

            string query = string.Format("select * from {0} " +
                                         "where {1} = '{2}';",
                                         obj.GetType().Name,
                                         props[0].Name,
                                         objectID);

            try
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                MySqlCommand    cmd    = new MySqlCommand(query, conn);
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        MapValueToType(props[i], rowData, reader, i);
                    }
                }
                conn.Close();
                return(rowData);
            }
            catch (MySqlException err)
            {
                Console.WriteLine(err);
                return(default(T));
            }
        }